Mini-Linter - Step 4 - help with correct sytax for increasing count in a nested loop

Hi all,

I’m trying to solve step 3 of the mini-linter project by using a nested loop:

https://www.codecademy.com/courses/introduction-to-javascript/projects/mini-linter

Here’s my code:

for (let i = 0; i < betterWords.length; i++){
 for (let j = 0; j < overusedWords.length; j++){
  if (betterWords[i] === overusedWords[j]){
  if (betterWords[i] === "really"){
    return reallyCount+= }
  else if (betterWords[i] === "very"){
    return veryCount+= }
  else if (betterWords[i] === "basically"){
    return basicallyCount+= }
  else resultArray.push(betterWords[i])}
  }
}

I’m running into syntax errors that I can’t move past. Please could somebody step in and let me know what I’m doing wrong?

Cheers!

I realised that I wasn’t instructing how much to add to the counts, so I changed to the below:

for (let i = 0; i < betterWords.length; i++){
 for (let j = 0; j < overusedWords.length; j++){
  if (betterWords[i] === overusedWords[j]){
  if (betterWords[i] === "really"){
    return reallyCount+= 1}
  else if (betterWords[i] === "very"){
    return veryCount+= 1}
  else if (betterWords[i] === "basically"){
    return basicallyCount+= 1}
  else resultArray.push(betterWords[i])}
  }
}

Now I just get a blank console when I run my code - even when trying to log the counts and arrays.

The demo uses a different method - would love to be hand-held through this method for my own knowledge.

Cheers!

Hi design4779428521, I’m assuming by looking at your code, you’re talking about step 4, not 3. Two things I noticed in your code:

return reallyCount+= 1
return veryCount+= 1
return basicallyCount+= 1

A return statement is unnecessary because you’re not inside a function. Even if you were inside one, a return statement means the rest of the code won’t be executed, and won’t work properly when you’re inside a loop.
It’s better to omit the return and just use the statement after the loop (While inside a function).
In this case just omitting the return should be enough.

 else resultArray.push(betterWords[i])}

Also, if I’m not mistaken, your else statement is compared with the inner if statement and not the outer.
Meaning it’s inside the outer if.

Thanks - sorry I took so long to get back to this, real life got in the way as it so often did.

I ended up with the following which seems to work fine:

for (let i = 0; i < betterWords.length; i++){
    for (let j = 0; j < overusedWords.length; j++){
        if (betterWords[i] === overusedWords[j]) {
            if (betterWords[i] === 'really') {
                reallyCount+= 1 }
        else if (betterWords[i] === 'very') {
                veryCount+= 1 }
        else if (betterWords[i] === 'basically') {
                basicallyCount+= 1 }
        }
    }
}

Thanks once again!

1 Like