Mini Linter - Step 4

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.

Consider that .filter takes a predicate function, which returns a boolean. Based upon that return, the tested value will be appended to the output array if the check yields true.

odds = [1,2,3,4,5,6,7,8,9].filter(x => x % 2)

//  [1, 3, 5, 7, 9]

Eg.

betterWords = storyWords.filter(x => ! unnecessaryWords.includes(x))

Recall that Array.forEach() has no return value so we cannot assign it to a variable. That is what results in undefined.

forEach performs an action on each item in an array without prejudice. What is your tooMuch variable meant to represent? How many overUsedWords are found in the betterWords array?

counter = 0
betterWords.forEach(x => {
    counter += overUsedWords.includes(x) ? 1 : 0;
})

That’s one example. Notice there is no return?

This topic was automatically closed 18 hours after the last reply. New replies are no longer allowed.