Step 8: Mini Linter Project JavaScript - No Error Message but it is not updating every other word

I am trying to change a word to something else every other time it is encountered. I dont get an error, but word changes out every time it is encountered, instead of every other time. What am I doing wrong?

//console.log(betterWords.join(' '));

const improveWords = betterWords.map(word => {
  let reallyCount = 0;
  let veryCount = 0;
if(word === 'really'&& reallyCount%2 !== 0){
     reallyCount = reallyCount + 1
     return word;
}else if(word === 'really'&& reallyCount%2 === 0 ) {
  reallyCount = reallyCount +1;
  return 'truly'
  }else if (word === 'very' && veryCount%2 === 0){
   veryCount = veryCount + 1;
   return 'vastly';
}else if (word === 'very' && veryCount%2 !== 0){
  veryCount = veryCount + 1;
  return word;
}else {return word;
}console.log(reallyCount);
});

console.log(improveWords.join(' '));

Check where you’re declaring the reallyCount and veryCount variables and consider when the value will be set to 0. Will the values ever be something other than 0 in your if conditionals?

More direct hint

The values of reallyCount and veryCount are being set to 0 every time map calls your arrow function to evaluate each word.

You need to move your variable declarations to before calling map so the values can be retained between words.

The values for reallyCount and veryCount are definitely more than zero. I think really is in the document at least 5 times.

reallyCount and veryCount are being set to 0 for every word since you are declaring them inside of the arrow function the map is calling. Therefore, as far as the if conditionals are concerned, they are 0.

You increasing the count by 1 inside gets undone on the next word since it gets set to 0 again before the values are evaluated.

Consider the following (your code, but I added a console.log before the if block

const testSentence = 'This is a really really really very very good sentence';

const betterWords = testSentence.split(' ');

const improveWords = betterWords.map(word => {
    let reallyCount = 0;
    let veryCount = 0;
    console.log(`reallyCount: ${reallyCount}, veryCount: ${veryCount}`);
    if (word === 'really' && reallyCount % 2 !== 0) {
        reallyCount = reallyCount + 1
        return word;
    } else if (word === 'really' && reallyCount % 2 === 0) {
        reallyCount = reallyCount + 1;
        return 'truly'
    } else if (word === 'very' && veryCount % 2 === 0) {
        veryCount = veryCount + 1;
        return 'vastly';
    } else if (word === 'very' && veryCount % 2 !== 0) {
        veryCount = veryCount + 1;
        return word;
    } else {
        return word;
    }
});


console.log(improveWords.join(' '));

The output of that is

reallyCount: 0, veryCount: 0
reallyCount: 0, veryCount: 0
reallyCount: 0, veryCount: 0
reallyCount: 0, veryCount: 0
reallyCount: 0, veryCount: 0
reallyCount: 0, veryCount: 0
reallyCount: 0, veryCount: 0
reallyCount: 0, veryCount: 0
reallyCount: 0, veryCount: 0
reallyCount: 0, veryCount: 0
This is a truly truly truly vastly vastly good sentence

However, if you moved your

    let reallyCount = 0;
    let veryCount = 0;

to before calling the map function so they aren’t declared inside the arrow function, then the output would be:

reallyCount: 0, veryCount: 0
reallyCount: 0, veryCount: 0
reallyCount: 0, veryCount: 0
reallyCount: 0, veryCount: 0
reallyCount: 1, veryCount: 0
reallyCount: 2, veryCount: 0
reallyCount: 3, veryCount: 0
reallyCount: 3, veryCount: 1
reallyCount: 3, veryCount: 2
reallyCount: 3, veryCount: 2
This is a truly really truly vastly very good sentence

You are a rock star! Thank you so much.

1 Like

You’re welcome. Variable scope can be tricky. I’m happy I was able to help