Mini Linter Step 8 - Help

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… :frowning: 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!

Throw in some console.logs to check that storyWord is being properly procured… if not you should consider revising your conditionals. If storyWord is being filtered correctly you should consider the structure of your map statements inside betterStory.

Nested maps can be convoluted, so sometimes it helps to start from the beginning and make sure your first cycle of iteration are giving you what you want – and then adding the second cycle.

1 Like

Hi @toastedpitabread and thanks for the tips!

I’ve actually tried throwing in some console.logs but still wasn’t able to find the bug. I have the impression that I might not be using the first map correctly because it’s not returning anything…

Anyway, I’ll try spending a bit more time testing out with console.logs to see if I can figure this out :exploding_head:

Cheers!

Try looking here for another way: