Mini Linter Javascript Project, step 4

Hey guys,

Another question here regarding the Mini-Linter project. The code prior works just fine, but I’ve run into a snag here at step 4.

I’ve found other ways of making it work by using forEach with 3 if statements to save time so this is more for curiosity’s sake. Why isn’t the below code working as expected? The code runs, no errors, but my counts have not incremented as expected (they show 0 , 0 , 0 for all three variables)

I’m not looking for the answer, just maybe a slight nudge in the correct direction. Thanks!!

Here’s the code I have so far:


let count0 = 0;
let count1 = 0;
let count2 = 0;

const overusedWordCount = betterWords.forEach(word => {
for (let i = 0; i < betterWords.length; i++) {
  if (i === "really"){
    return count0++;
    } else if (i === "very"){
      return count1++;
      } else if (i === "actually"){
        return count2++;
} else {
  continue;
}
}});

console.log(count0);
console.log(count1);
console.log(count2);

Hello,

Since you’re only looking for a nudge, then keep in mind that i is a number and will never meet those conditions.

1 Like

Hi.

Hmmm I don’t understand, what do you mean? I’d be iterating through an array of strings. How is i a number?

Thanks,

G

Add a console.log(i); at the beginning of your for loop. I think you intend to use it in another way

ehhhhhhhhh - I still don’t understand.

Did you try adding console.log(i); at the beginning of your loop, like selectall suggested?
If you did, you saw a lot of numbers, that’s because i is your iterator in the for loop: it’s initialized at the beginning (let i = 0;) and it’s incremented at the end (i++) until a condition is met (i < betterWords.length). So you’ll see a list of 0 till the last index of the betterWords array, and you’ll see that list for every word in betterWords because of the .forEach loop (So if betterWords.length is 15 you’ll see a list of 0-14 for 15 times).

Which means, when the computer is checking your conditions, it’s comparing a number to a string.
In i === "really", i is a number so the condition will not evaluate to true, and your counters will not work correctly.

If you want to compare words in the betterWords array, you should use i to indicate the index, like this:

Solution

betterWords[i] === "really".

2 Likes