I’m confused on Step 4 of the [Mini Linter] javascript iterators project.(https://www.codecademy.com/paths/web-development/tracks/web-dev-js-arrays-loops-objects/modules/learn-javascript-iterators/projects/mini-linter)
As this is a project related to iterators I’m trying to use the .forEach() method combined with if else if statements.
Here is the code:
let overusedWords = ['really', 'very', 'basically'];
let unnecessaryWords = ['extremely', 'literally', 'actually' ];
//make an array from the string assigned to the variable story
const storyWords = story.split(' ');
//show the number of items in the new array
console.log(storyWords.length);
// make a new array by filtering out unnecessary words
const betterWords = storyWords.filter(word => {
if (unnecessaryWords.includes(word)) {
return;
} else {
return word;
}
});
//show and compare the number of items in betterWords vs. storyWords
console.log(betterWords.length);
//count the number of times an over used word occurs
const tooMuch = betterWords.forEach(item => {
let counter = 0;
if(overusedWords[0] === item) {
counter++;
} else if (overusedWords[1] === item) {
counter++;
} else if (overusedWords[2] === item) {
counter++;
} else {
return counter;
}
});
console.log(tooMuch);
I don’t understand why the console.log(tooMuch)
returns undefined. I’ve tried several variations of the conditional returns such as: return counter ++, return counter +=, return counter +1.