Using the filter() method, how would I remove every other occurrence of an element, rather than all occurrences?

Apologies if I am not doing this right, it’s my very first post here and I’m as green as they come with coding overall. I would like to use the filter() method on the storyWords array and only filter out every other occurrence of the strings stored in the ‘overusedWords’ variable, as opposed to all occurences.

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 wordCount = storyWords.length;

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


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

Answer

We can count up the number of overused and unnecessary words, and only let every second one through.

We do this by maintaining a count of the number of occurrences for each word, then letting it through on every second occurrence.

Update: I’ve updated to let the first, third occurrence etc through rather than the second, fourth etc. I think this is more like what you wish. You can switch between the two behaviours by checking for a mod 2 of either 1 or 0 respectively.

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 wordCount = storyWords.length;

let wordsRemoved = {};
let wordOccurrences = {};

let betterWords = storyWords
  .filter(word => {
    let match = unnecessaryWords.includes(word) || overusedWords.includes(word);
    if (match) {
        wordOccurrences[word] = (wordOccurrences[word] || 0) + 1;
    }
    // let a word through either if it's not a match or the occurrence count is odd.
    // Change === 1 to === 0 to let even occurrences rather than odd occurrences throug
    if (!match || ((wordOccurrences[word] % 2) === 1)) {
        return true;
    }
    wordsRemoved[word] = (wordsRemoved[word] || 0) + 1;
    return false;
  })

console.log("Total input words:", wordCount );
console.log("Total output words:", betterWords.length);
console.log(`Word occurrences:`, wordOccurrences);
console.log(`Words removed:`, wordsRemoved);
console.log(betterWords.join(' '));