Mini Linter: Iterating "overusedWords" with FOR Loops


#1

Hi there,

I have a a few questions about point 4 in the Mini Iterator project:
https://www.codecademy.com/courses/learn-javascript-iterators/projects/mini-linter?course_redirect=introduction-to-javascript

First Question:

I made this code to help count the number of overusedWords:

for (let betterIndex = 0; betterIndex <= betterWords.length; betterIndex++) { //runs through the list of betterWords
  for (let overIndex = 0; overIndex <= overusedWords.length; overIndex++) { //runs through list of overusedWords
    if (betterWords[betterIndex] === overusedWords[overIndex]) { //remaining code will match appropriate items and increase increment when matching
    countReally ++;
    }
    else if (betterWords[betterIndex] === overusedWords[overIndex]) {
    countVery++;
    }
    else if (betterWords[betterIndex] === overusedWords[overIndex]) {
    countBasically++;
    }
  }
} 

The result is that I get a count of 9 for countReally.

However, when I remove the FOR loop for overusedWords like such:

for (let betterIndex = 0; betterIndex <= betterWords.length; betterIndex++) {
    if (betterWords[betterIndex] === overusedWords[0]) {
    countReally ++;
    }
    else if (betterWords[betterIndex] === overusedWords[1]) {
    countVery++;
    }
    else if (betterWords[betterIndex] === overusedWords[2]) {
    countBasically++;
    }
}

The result is 2 for countReally, 5 for countVery, and 1 for countBasically. Although this works, I’d rather get both FOR loops working. Trying to think ahead for when the array might be larger than three items.

Second Question:

I’m trying to find a more efficient way to iterate through this list, and I was wondering if there was an iterator like .map(), .include(), and etc., that might be able to help shorten the code. Is there a way to do it efficiently with the iterators that we’ve learned through this lesson? If not are there any other iterators we can utilize?

Thanks in advance!
~Mac


#2

using a second loop won’t work. You could make it work, but its really tricky. But look at your if and else if conditions, they are all the same, so it will always evaluate to true at if, and else ifs will never run

not really, if we just had to count the number of overused words, then yes (using filter, includes and length)


#4