Hi everyone.
I’m trying to accomplish Step 8.1 of the Mini Linter JS project. Essentially what I’m trying to do is to remove every other instance of each word in overusedWords from storyWords, i.e:
I want to take this:
[
‘This’, ‘is’,
‘a’, ‘really’,
‘really’, ‘really’,
‘very’, ‘very’,
‘good’, ‘sentence.’
]
and create a new array with this:
[
‘This’, ‘is’,
‘a’, 'really,
‘really’, ‘very’,
‘good’, ‘sentence.’
]
but it’s not working… check below the outputs I’m getting.
let story = 'This is a really really really very very good sentence.';
let overusedWords = ['really', 'very'];
let storyWords = story.split(' '); // Splits story into an array with each word per index
console.log(storyWords.length); // Prints 10
let betterStory = overusedWords.map((overusedWord) => { // Iterating through overusedWords
let counter = 0;
storyWords.map((storyWord) => { // Iterating through storyWord
if (storyWord === overusedWord && counter % 2 !== 0) { // If words match and counter is odd increment counter and don't return word (because counter starts at 0)
counter += 1;
} else if (storyWord === overusedWord && counter % 2 === 0) { // Otherwise, if words match and counter is even increment counter and return word
counter += 1;
return storyWord;
} else { // Otherwise, i.e, words don't match, return word
return storyWord;
}
});
});
console.log(betterStory.length); // Prints 3
console.log(betterStory); // Prints [ undefined, undefined, undefined ]
Thanks in advance!