Mini Linter - Step 3

Hello,

I have a problem with my code in the mini linter exercise Step 3.

The exercise step asks for the following:

There is an array of words that are unnecessary. Iterate over your array to filter out these words. Save the remaining words in an array called betterWords. There are several ways that you could achieve this.

and my code to solve it is the following:

const betterWords = storyWords.filter(word => {
  for (let i = 0; i < unnecessaryWords.length; i++){
    if (word !== unnecessaryWords[i]){
      return word;
    }
  }
})  

It seems that there are 188 words in my storyWords array length and 6 words in my unnecessaryWords array length, so my betterWords Array length should be 182 but its 185. As i try log in the console my betterWords array i can still see some of the words that should be filtered out.

I tried some code with the include method too as the hint suggests but the final result is always the same.
Any ideas on what am i missing?

Hi @giga1909313137
let’s say the word you filter is ‘hello’ and this is also part of the array unnecessaryWords, but the first word in your array unnecessaryWordsis ‘bye’. In this case, the condition

is fulfilled and the filter returns the word without checking the rest of the array.

After a little bit of experimentation i came up with this:

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

and still was missing something as it didn’t work either.
So i kept looking and i tried this:

  return word =! unnecessaryWords.includes(word);

and finally worked :’)

The first try got me again 185 words on my array length and confused me, then i searched for the correct syntax of the NOT operator and got it!

Ok, you’re using the assignment operator, so you’re not returning a word but a boolean value.
What you’re doing is this:

return true; // if word is not included
! unnecessaryWords.includes(word); // === true if word is not included
word = true // assignment if word is not included

You could make it simple like:
const betterWords = storyWords.filter(word => !unnecessaryWords.includes(word));

Yeah,

thanks a lot for your help, you really helped me alot with your first reply =)

1 Like