Mini Linter Step 4 Improvement

Hello everyone!

I’m currently working my way through the Mini Linter Project and would like help at improving something on step 4.
Here is my current code:

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"];

// Splits story, converting it into an array with each word per index and logs its length (Step 1)
let storyWords = story.split(" ");
console.log(storyWords.length);

// Filters through storyWords and returns an array with the words that aren't included in unnecessaryWords, then logs its length
let betterWords = storyWords.filter((betWord) => {
  return !unnecessaryWords.includes(betWord);
});
console.log(betterWords.length);

// Creates a counter for each word in in overusedWords and sets it to 0
let reallyCounter = 0;
let veryCounter = 0;
let basicallyCounter = 0;

storyWords.forEach(word => { //Iterates through storyWords
  if (overusedWords[0] === word) { // Checks if the current word is equal to overusedWords[0] and if so increments the respective counter
    reallyCounter += 1;
  } else if (overusedWords[1] === word) { // Checks if the current word is equal to overusedWords[1] and if so increments the respective counter
      veryCounter += 1;
  } else if (overusedWords[2] === word) { // Checks if the current word is equal to overusedWords[2] and if so increments the respective counter
      basicallyCounter += 1;
  }
});

// Logs the counters
console.log(`'${overusedWords[0]}' counter: ${reallyCounter}`);
console.log(`'${overusedWords[1]}' counter: ${veryCounter}`);
console.log(`'${overusedWords[2]}' counter: ${basicallyCounter}`);

Now this is all fine and working according to what the Step 4 asks for. However, this will only work and make sense (due the counter variables’ names) if the overusedWords array has exactly those 3 elements. What would be the best way to make the code more generic so it would still work (and make sense) if we changed the overusedWords array, i.e change its elements and its number of elements?

I hope I’ve made myself clear enough.
Thanks a lot!

Make a hashmap:

test = {"really": 1, "very": 2}

//if a new word appears (i.e. it's not already in your hashmap)
test["newWord"] = 1 // adds the new word to the hashmap

console.log(test);
//{ really: 1, very: 2, newWord: 1 }

this way you can have a counter for all words and if they exceed a certain number you can filter them out with your code.

This worked perfectly! Thanks!

Just for completeness this is how I coded it:

// Creates an empty hashmap
let test = {};

//Iterates through storyWords
storyWords.forEach((storyWord) => {
    // Iterates through overusedWords
    overusedWords.forEach((overusedWord) => {
        // Checks if current overusedWord is equal to storyWord
        if (overusedWord === storyWord) {
            // If current word exists in the hashmap increments 1, otherwise creates the new property and sets it to 1
            storyWord in test ? test[storyWord] += 1 : test[storyWord] = 1;
        }
    });
});