Step 3: No error message, but code stil didn't do what it was supposed to

While I was working to filter out unnecessary words from the storyWords array using the .filter-method, I at one point tried to use an if-/else-function as callback. The code seemed to work, but when I printed the betterWords array to the console I saw it still containing words I was supposed to have gotten rid of.

I have since looked at the video and I see the benefits of using the .includes-method. But I somehow still wonder why my first amateur try didn’t work out. (I.e., no error code was printed, but “extremely”, “actually” and “literally” still showed up in the filtered array.)

Anyone who can explain why that was?


link to code:

If your code is able to determine true/false after looking at the first word, then what did it do with the rest of the words? Presumably not a whole lot. A loop that makes one iteration isn’t really a loop at all, there’s no looping going on.
If you meant to compare to each word in the list then you’ll need to do that before exiting your function and presumably combine the results of the comparisons in some way into one consolidated answer.

Tagging on to what @ionatan said, the return keyword exits a loop, so I think what your code is doing is comparing each word in storyWords to ‘extremely’ (because that’s unnecessaryWords[0]). It returns either true or false, exits the for loop, then moves on to the next word in storyWords. That means that the other words in unnecessaryWords never get checked.

1 Like

The return keyword exits the loop? I did not know that! (Only knew of the break keyword.) Thank you, both!