Why does this code not work?

In this project for the third task, I know I can use include() method but I tried something different but I couldn’t manage to understand why my code didn’t work.
Here is my 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(" ");

const betterWords = storyWords.filter(word => {

unnecessaryWords.forEach(word2 => {

    if(word != word2){

        return word;

    }

    

});

});

console.log(betterWords);
betterWords returns an empty array but I couldn’t understand why?

Hi @yigitceli
The const betterWordsnever receives the returned word, because you are returning it to the calling function, which is the one you call in your filter.
You could push the words to the array instead:


const betterWords = [];
storyWords.filter((word, index) => {
    unnecessaryWords.forEach(word2 => {

    if(word != word2){
        betterWords.push(word);
    }
});
});

Thank you so much, i have another code which does not work correctly in the same project,

Here is my 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(" ");

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

});

const times = betterWords.reduce((acc,word) => {

for(let i = 0;i < overusedWords.length; i++)

{

    if(word.toLowerCase() === overusedWords[i]){

        return acc + 1;

    }else{

        return acc;

    }

}

})

console.log(times);

Here in this code, i think it works correctly until it logs first element of betterWords aray which is “Last”. In short it returns Last11. How can i get rid of Last ?

By not returning it. You want the amount of hits, right? Now you’re exiting the loop after the first iteration and just concatenate ‘1’ and ‘1’.
Put console.log(acc + 1); into your if statement and see what it does exactly.
If you want the amount, add a variable counter, reassign it in the if statement and return outside the for loop.