Mini Linter Step 3

For the step asking to filter words in the unnecessaryWords array, why can’t I just compare storyWords to unnecessaryWords like below:

let betterWords = storyWords.filter(x=> {
x !== unnecessaryWords
return x;
})

Is return !unnecessaryWords.includes(x) the only way to do this?

1 Like

The example you have isn’t really checking that the word is the same as the words in unnecessaryWords
that’s because x !== unnecessaryWords is just there and then you return x.
You should put it in an if statement so it would return true if it isn’t the same like:

let betterWords = storyWords.filter(x=> {
  if (x !== unnecessaryWords) {
    return true;
  } else {
    return false;
  }
});

The other problem is that, since unnecessaryWords is an array, you can’t check if
x !== unnecessaryWords, because this way you’re checking if ["word1", "word2", "word3"] is the same as "word", to do this correctly you would have to loop through unnecessaryWords, either with forEach or with a simple for for:

let betterWords = storyWords.filter(x=> {
  unnecessaryWords.forEach(word =>{
    if (x !== word) {
      return true;
    } else {
      return false;
    }
    /* or you could do simply:
    return x !== word;
   */
  });
});

Filter loops through an array you called the function on, and then it calls your callback function with each word, if it returns true it puts it into the new array, if it returns true, then it won’t.
Then if it has looped through the array it will return the newly created array with the filtered values

I hope this helps :grinning:

2 Likes

This is related to Step 3 of the Mini Lintner project
I used !== as my bang operator but in the video he uses !
They both worked but I’m curious how you decide which one to use?