Mini Linter Solution Suggestions

Hi there!

After reviewing the Mini Linter solution under Get Unstuck, I wasn’t too crazy about having to hard-code the individual overused words inside conditional statements. Besides, I certainly don’t want as many conditional statements as there are words in the overusedWords array.

Here’s a general solution that capitalizes on the various lessons and the recommended Mozilla documentation.

// Count total number of occurrences of overused words.
// Initialize count as an empty array.
let overusedWordCount = ;

storyWords.forEach(word => {
for (i = 0; i < overusedWords.length; i++) {
if (isNaN(overusedWordCount[i])) {
overusedWordCount.push(0);
}
if (word === overusedWords[i]) {
overusedWordCount[i]++;
}
}
})

// Display total number of occurrences of overused words.
overusedWords.forEach(word => {
const i = overusedWords.indexOf(word);
console.log(word + ': ’ + overusedWordCount[i]);
})

3 Likes

I know you posted this a few months ago, but thank you so much! I, too, was bothered by the multiple conditionals in the solution and KNEW that there must be a way to generalize it. I struggled for a few hours trying to come up with a better solution, but couldn’t get mine to work. Your solution helped me realize what, exactly, I wanted to do with my code and then I was able to fix my errors. Thank you again!

2 Likes

Thank you, I also sat for a long time, refusing to hardcode it like that. You helped guiding me to a better solution :slight_smile:

May the sun forever shine wherever you are!

Thank you so much. I feel more at ease knowing there’s a better way than hard-coding the individual overused words!

I used the filter method to avoid hard-coding. The filter method creates the new array howManyOverusedWords, then I console logged the length of this array.

const howManyOverusedWords = storyWords.filter( word => {
    if (overusedWords.includes(word) === true){
      return word
      }
  }
)
console.log(howManyOverusedWords.length)

I think this worked, I made an array filled with each instance of the overused words in the story, then I counted the length of that array.

2 Likes

Above is the predicate. It does not need to be enclosed in extra logic, it is the logic.

a.filter(w => b.includes(w))
2 Likes

How do I tie the return logic to this? Would I just go

a.filter(w  => b.includes(w)){
return w
}

The concise body arrow function has an implicit return when the body is a simple expression.

That is a subsequent code block (meaning after the filter). It will throw an error, return outside a function or some such. Everything we need is right in the filter expression.

array.method(param => otherArray.method(arg))

It helps to break down what the filter callback is doing…

w is the iteration variable

.filter() has a built in iteration loop that goes through array a, with w being one element at a time. Each element is passed to the .includes() method on array b. If found, that value is passed up to the filter to append to the return array.

2 Likes

This seems to identify how to list the words that we want to remove. I don’t understand how you go from that to removing the words included. I tried the following but it didn’t work…

const betterWords = storyWords.filter (word => word != unnecessaryWords.includes (word));

We wouldn’t be testing equality, but negating the expression that follows.

    negation
       \
word => ! unnecessaryWords.includes(word)
                          \
                        boolean

The negation says to, “Ignore if true.”

1 Like

I have been trying to complete the extra tasks with some difficulty. I believe I found a way to write a function that find the word that appears the greatest number of times but I don’t understand how it works. Any help on how to understand it would be greatly appreciated! This is the code I have:

function mostFrequent(arr) {
arr.sort();
let maxCount = 1, res = arr[0];
let currentCount = 1;
for (let i = 1; i < arr.length; i++) {
  if (arr[i] === arr[i - 1])
  currentCount++;
  else {
    if (currentCount > maxCount) {
      maxCount = currentCount;
      res = arr[i - 1];
    } currentCount = 1
  }
} if (currentCount > maxCount)
        {
            maxCount = currentCount;
            res = arr[n - 1];
        }
        return res;
    }
console.log(mostFrequent(storyWords))
mostFrequent(storyWords);
console.log(mostFrequent(storyWords))
                         ^

ReferenceError: storyWords is not defined
let story = 'Last weekend, I took literally the most beautiful bike ride of my life. The route is called "The 9W to Nyack" and it actually stretches all the way from Riverside Park in Manhattan to South Nyack, New Jersey. It\'s really an adventure from beginning to end! It is a 48 mile loop and it basically took me an entire day. I stopped at Riverbank State Park to take some extremely artsy photos. It was a short stop, though, because I had a really long way left to go. After a quick photo op at the very popular Little Red Lighthouse, I began my trek across the George Washington Bridge into New Jersey. The GW is actually very long - 4,760 feet! I was already very tired by the time I got to the other side. An hour later, I reached Greenbrook Nature Sanctuary, an extremely beautiful park along the coast of the Hudson. Something that was very surprising to me was that near the end of the route you actually cross back into New York! At this point, you are very close to the end.'; let overusedWords = ['really', 'very', 'basically']; let unnecessaryWords = ['extremely', 'literally', 'actually' ]; let storyWords = story.split(' '); const betterWords = storyWords.filter(word => word !== unnecessaryWords[0] && word !== unnecessaryWords[1] && word !== unnecessaryWords[2]); let reallyCount = 0; let veryCount = 0; let basicallyCount = 0; let overusedWordCount = 0; for (word of storyWords) { if (word === 'really') { reallyCount += 1; } else if (word === 'very') { veryCount += 1; } else if (word === 'basically') { basicallyCount += 1; } overusedWordCount = reallyCount + veryCount + basicallyCount; } let sentenceCounter = 0; for (word of storyWords) { if (word.includes('.') || word.includes('!')) { sentenceCounter += 1; } } console.log(storyWords.length); console.log(sentenceCounter); console.log(overusedWordCount); const logInfo = (storyWords, sentenceCounter,overusedWordCount) => { console.log('Word count: ' + storyWords.length + '. Sentence count: ' + sentenceCounter + '. Overused word appearances: ' + overusedWordCount + '.') } logInfo(storyWords, sentenceCounter, overusedWordCount) //console.log(betterWords.join(' ')); function mostFrequent(arr) { arr.sort(); let maxCount = 1, res = arr[0]; let currentCount = 1; for (let i = 1; i < arr.length; i++) { if (arr[i] === arr[i - 1]) currentCount++; else { if (currentCount > maxCount) { maxCount = currentCount; res = arr[i - 1]; } currentCount = 1 } } if (currentCount > maxCount) { maxCount = currentCount; res = arr[n - 1]; } return res; } console.log(mostFrequent(storyWords)) mostFrequent(storyWords);

I understand now! Thanks so much both @henry_dodder and @mtf ❤️‍🔥

let overUsedWordsCount = storyWords.filter( word => 
  overusedWords.includes(word)
);
1 Like

I agree. They limited the program to those only words. I wrote it so that the contents of overusedWords and unnecessayWords arrays could change and the program would still work. I am surprise their solution did not.