Stuck on Mini Letter

Hello,

I am stuck on question 3

" 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."

for the life of me I dont know that syntax to use with the .filter iterator to compare two separate arrays containing strings and subtract the strings from one array from the other when they match. After a while I went off script and tried using a nested for loop but that gave me a satanic like error message. here is what I have so far:

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

let storyWords = story.split(’’);

let betterWords = ;
for(let i = 0; i < storyWords.length; i++){
for(let j = 0; i < unnecessaryWords.length; j++){
if(storyWords[i] !== unnecessaryWords[j]){
betterWords.push(storyWords[i])
}
}
};

console.log(betterWords);

The clue to the expected solution is the filter iterator. This unit is about iterator methods that can shoulder a lot of the load so we don’t need for loops.

array.iterator(callback)

Iterators generally return a new array, not act upon the array in context.

The idea is that given an array of data and a function that can iterate that data, missing nothing, what function do we wish to perform on that data? That is the callback function. It can be anything.

Some iterators are read-only, such as array.forEach. Some rely on a function to perform actions on data members and cache them to a new array, such as array.map; and some rely upon a predicate function to determine what gets written to the new array. array.filter is one of them.

array.filter(predicate)

A predicate function returns a boolean. Write a function that tests if a term is a member of a sequence; return the boolean that satisfies your requirements.

Above we see that the outcome expected is an array free of unnecessary words. We wish to filter all words that are not a member of the unnecessary words array.

That suggests an iterator in the callback. array.includes. Another predicate function.

Piecing this together on two imaginary arrays, foo and bar, we will make sure that foo has none of the members of bar.

foo.filter(x => ! bar.includes(x))

Consider,

 > foo = [1,2,4,6,8,9]
<- (6) [1, 2, 4, 6, 8, 9]
 > bar = [1,9]
<- (2) [1, 9]
 > foo.filter(x => ! bar.includes(x))
<- (4) [2, 4, 6, 8]
 > 

Well, that’s not entirely true,

 > foo
<- (6) [1, 2, 4, 6, 8, 9]

unless we assign the outcome to foo.

 > foo = foo.filter(x => ! bar.includes(x))
<- (4) [2, 4, 6, 8]
 > foo
<- (4) [2, 4, 6, 8]

Witness! No loops.