Iterators Mini Linter-Step 3 why is my code giving an incorrect answer?


#1

Hi friends, while I am working on step 3 to get rid of the unnecessary words, I use the code below. The goal is to return the words that are not in the unnecessary list to be saved in betterWords. But it turns out that betterWords still contains some of those unnecessary words. Can someone help to explain why this is wrong? Thanks in advance!

let betterWords = storyWords.filter(function(item) {
  for (let unnecessaryWordsIndex = 0; unnecessaryWordsIndex < unnecessaryWords.length; unnecessaryWordsIndex ++) {
  return  item != unnecessaryWords[unnecessaryWordsIndex];
  }
});

#2
storyWords.filter(x => ! unnecessaryWords.includes(x));

We used the iterator built into the filter() method to cycle through the primary array and the iterator built in to the includes() method to cycle through the secondary array…


#3

So, first of all, you should refactor the function. It is more efficient to write code that way. Takes less time.

You could create a for loop, though the Mini Linter is about Iterators. So its all about creating time efficient code. Less writing. So your code would use iterators instead of loops and such. You started correctly with:

let betterWords

This will become the array your new list of words will be stored in. You make let betterWords an array by using the filter Iterator. Like so:

let betterWords = storyWords.filter(Item)

The filter will always create a new Array without those Words/Items you want to have filtered out. It’s like the filter is a bouncer and promoter in one. It has the instructions to go inside other clubs(other Arrays) and invite people to your party(new Array) but only if those people have the correct dress code.

You can either tell it to invite everyone with a party hat or tell it not to invite anyone without a party hat. This way of wording will essentially invite the same people though depending on what information you already got it can save you time to say either one version of this sentences in code.

Now the filter is a powerful bouncer and promoter, if it invites someone they have to come. It’s like a spell. Though the club owner doesn’t like that. Your promoter is stealing his customers. So your promoter decides to clone everyone she wants to invite and invites the clone instead. That way the club owner is happy and your party gets going because the right crowd is joining.

The .filter will do exactly the same. It will look for Words/Items and put them inside a new array(your party) without disturbing the old array(the club) by cloning the Words/Items when it puts them into the new array. The new Array in our case is betterWords(your party) and the old Array is storyWords(the club).

Next: You want to tell your filter what words you do want or what words you do not not want to be cloned and put inside the new array. Since all Words in the unnecessaryWords Array are those that are unwanted at your party(the betterWords Array) you should tell your .filter to let everyone in but those inside the unnecessaryWords Array.

So:

let betterWords = storyWords.filter(Item => !unnecessaryWords.includes(Item));

Item is just a placeholder you can call it “Word”,“Item”,“MattersNon” or “IloveIceCream”. It just stands for every individual word the filter will interview and look at. In a conversation with an actual bouncer, you would probably say “Look out for people that are too drunk, we don’t want them in the club.” The word “people” is just a placeholder for every individual person that wants to get in. So instead of saying:

“Look out for tall black haired guys that are too drunk, we don’t want them in the club.”
“Look out for short blond haired girls that are too drunk, we don’t want them in the club.”
“Look out for skinny brown haired guys that are too drunk, we don’t want them in the club.”
“Look out for short blue-eyed girls that are too drunk, we don’t want them in the club.”
“Look out for…”
You just say “people”. It saves you 100 years on going through all different variations of people that might show up that night and are too drunk to get in.

Now that we look at this code again we understand most of it already:

let betterWords = storyWords.filter(Item => !unnecessaryWords.includes(Item));

The .includes Iterator checks whether or not one thing is the same as another and then returns a boolean. Saying true or false.

So since “Item” is just a placeholder for every single Word from the storyWords array it checks whether or the word in question is equal to the word in the unnecessaryWords Array.

Here is the catch. Remember earlier when we decided in our case it is best to tell the filter not who to clone and invite but rather who not to clone to clone and invite. We did this because we already had a list on who not to invite, the unnecessaryWords array. So we use the “!” before the unnecessaryWords array. This means “if it is NOT than…”
So:

!unnecessaryWords.includes(MattersNon));

Means if unnecessaryWords is not the same as any one word that is interviewed by the bouncer(the filter) right now than let it in. Otherwise, tell it to go away.

let betterWords = storyWords.filter(MattersNon => !unnecessaryWords.includes(MattersNon));

If you were to translate the entire code above to tell a bouncer what he is suppose to do that night. You would say.

I am having a party(betterWords) I want you to go to the doppest club in town(storyWords) and look for party people that know how to party. I want you to look at every individual person(MattersNon) you see in there(storyWords) and see if they are NOT part of the Staff(!unnecessaryWords). Than clone them and tell their clones to come to my party (betterWords).

If you have questions let me know. I’ll see if I can answer them.


#4

Thank you for the very detailed explanation. It helps a lot!