Mini Linter Project Step 3: Unexpected number of elements being removed from array

The following is my current code:

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

const storyWords = story.split(' ');

console.log(storyWords.length);

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

console.log(betterWords.length);

Which when ran returns the following:

188
179

However, the expected output is:

188
182

In fact when I remove the ! in the if statement, console.log(betterWords.length); returns 6 as is expected. I don’t understand why ‘not-ing’ it doesn’t return 188 - 6 as it’s length

link to the exercise

You are returning word in your if statement.

Certain values are classed as truthy or falsey.

There are three emtpy string values that are returned from the split() and theses are seen as a false value by the filter() and are being excluded from the number as well as the unnecessaryWords.

const storyWords = story.split(' ');
let unnecessaryWords = ['extremely', 'literally', 'actually'];

const betterWords = storyWords.filter(word => {
	if (!(unnecessaryWords.includes(word))) {
		if(!word) {
			// Tests for word falsyness
			console.log(word);
		}
	}
});

You could have used this instead;

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

The if statement is effectively a second test for the same thing, as unnecessaryWords.includes(word) returns a true or false value on its own.

const betterWords = storyWords.filter(word => !unnecessaryWords.includes(word));
1 Like

Ah! I didn’t even consider falsey values to be at play here! Thanks for showing me that method on how to test for falsey values!

Also I clearly misunderstood how to properly use filter()! I didn’t realise it used a boolean as its parameter! I see why the if statement is unecessary due to that now.

Thank you! I understand exactly what went wrong and I now understand filter() better now!