sikarak
sikarak

Reputation: 108

Filter method on array doesn't work as excepted

I'm making an exercise (mini-linter) on iterators from codecademy.

i have an array of words and a second one containing forbidden words. I try to filter out a new array excluding the forbidden words. But after displaying the array in console, the forbidden words still remaining.. Someone can help me to figure it out ?

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 unnecessaryWords = ['extremely', 'literally', 'actually'];
console.log(story.length);
console.log(story);
let storyWords = story.split(" ");

let betterWords = storyWords.filter(function(word) {
	return word != unnecessaryWords.includes(word);
});

console.log(betterWords.length);
console.log(betterWords.join(' '));

Thanks a lot :)

Upvotes: 1

Views: 210

Answers (4)

Firas Omrane
Firas Omrane

Reputation: 932

You have to use return !unnecessaryWords.includes(word);.

word is a String and unnecessaryWords.includes(word); returns a boolean so you are comparing word with either the true or false

for example, your storyWord array contains empty strings and we know that '' == false will return true so empty strings will be removed from your array

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 unnecessaryWords = ['extremely', 'literally', 'actually'];
//console.log(story.length);
//console.log(story);
let storyWords = story.split(" ");

let betterWords = storyWords.filter(function(word) {
	return !unnecessaryWords.includes(word);
});

console.log(betterWords.length);
console.log(betterWords.join(' '));

Upvotes: 0

nitte93
nitte93

Reputation: 1840

unnecessaryWords.includes(word) returns a boolean true/false.

You don't need to compare word != unnecessaryWords.includes(word)

You are already checking it with unnecessaryWords.includes(word)

Just return return !unnecessaryWords.includes(word);

Instead of word != unnecessaryWords.includes(word)

Upvotes: 0

Ankit Agarwal
Ankit Agarwal

Reputation: 30729

You can use return !unnecessaryWords.includes(word);

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 unnecessaryWords = ['extremely', 'literally', 'actually'];
console.log(story.length);
console.log(story);
let storyWords = story.split(" ");

let betterWords = storyWords.filter(function(word) {
	return !unnecessaryWords.includes(word);
});

console.log(betterWords.length);
console.log(betterWords.join(' '));
console.log(betterWords.length);

Upvotes: 1

Nina Scholz
Nina Scholz

Reputation: 386868

You need to return just the negated result of include check, not the result of another check with the word.

return !unnecessaryWords.includes(word);

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 unnecessaryWords = ['extremely', 'literally', 'actually'];
console.log(story.length);
console.log(story);
let storyWords = story.split(" ");

let betterWords = storyWords.filter(function(word) {
    return !unnecessaryWords.includes(word);
});

console.log(betterWords.length);
console.log(betterWords.join(' '));

Upvotes: 2

Related Questions