Mini Linter - Step 4

My i isn’t iterating and returning 0 to the console. Please see my code below.

let story = 'Last weekend, I took literally the most beautiful bike ride of my life. The route is called "The 9W to Nyack" and it actually stretches all the way from Riverside Park in Manhattan to South Nyack, New Jersey. It\'s really an adventure from beginning to end! It is a 48 mile loop and it basically took me an entire day. I stopped at Riverbank State Park to take some extremely artsy photos. It was a short stop, though, because I had a really long way left to go. After a quick photo op at the very popular Little Red Lighthouse, I began my trek across the George Washington Bridge into New Jersey.  The GW is actually very long - 4,760 feet! I was already very tired by the time I got to the other side.  An hour later, I reached Greenbrook Nature Sanctuary, an extremely beautiful park along the coast of the Hudson.  Something that was very surprising to me was that near the end of the route you actually cross back into New York! At this point, you are very close to the end.';

let overusedWords = ['really', 'very', 'basically'];

let unnecessaryWords = ['extremely', 'literally', 'actually' ];

let storyWords = story.split(' ');


let betterWords = storyWords.filter(better => {
  return better != unnecessaryWords;
});

let i = 0;
if (storyWords === 'really'){
   while (i++);
} else if (storyWords === 'very'){
  while (i++);
} else if (storyWords === 'basically'){
  while (i++);
}
console.log(i);


2 Likes

Have a look at how you’re using while. The syntax for a while loop is:

let i = 0;
while (i<5){
//do something
i++;//make sure to increment it, otherwise you'll get an infinite loop.
}
1 Like

So, do I remove the ifs and just leave while? The instructions said to use an if...else statement.
Mini Linter

I’ve changed it to the following but the code is still logging 0 to the console.

let story = 'Last weekend, I took literally the most beautiful bike ride of my life. The route is called "The 9W to Nyack" and it actually stretches all the way from Riverside Park in Manhattan to South Nyack, New Jersey. It\'s really an adventure from beginning to end! It is a 48 mile loop and it basically took me an entire day. I stopped at Riverbank State Park to take some extremely artsy photos. It was a short stop, though, because I had a really long way left to go. After a quick photo op at the very popular Little Red Lighthouse, I began my trek across the George Washington Bridge into New Jersey.  The GW is actually very long - 4,760 feet! I was already very tired by the time I got to the other side.  An hour later, I reached Greenbrook Nature Sanctuary, an extremely beautiful park along the coast of the Hudson.  Something that was very surprising to me was that near the end of the route you actually cross back into New York! At this point, you are very close to the end.';

let overusedWords = ['really', 'very', 'basically'];

let unnecessaryWords = ['extremely', 'literally', 'actually' ];

let storyWords = story.split(' ');


let betterWords = storyWords.filter(better => {
  return better != unnecessaryWords;
});

let i = 0;
while (storyWords === 'really'){
   i++;
} while (storyWords === 'very'){
   i++;
} while (storyWords === 'basically'){
   i++;
}
console.log(i);


The way I read this, the while loops will never trigger. As storyWords is equal to a long array of words, it is not equal to the single words 'really' or 'very' or 'basically' at any point.

That being the case, your code will set i = 0 then evaluate:

false
false
false

Then log i without incrementing. Is there another way you can get the code to look at each element of storyWords instead?

So I chaned it to and else…if statement but still logging 0!

if (storyWords === 'really'){
   i++;
} else if (storyWords === 'very'){
   i++;
} else if (storyWords === 'basically'){
   i++;
}
console.log(i);


That’s still checking if the entire storyWords array is equal to a single word though.

You could use a .forEach() method to iterate through each word instead, or a for loop. Either method would need to wrap around your entire else if statements.

Eg:

Let i = 0;
storyWords.forEach(word => {
  if (word  === 'really'){
     i++;
  } else if (word === 'very'){
     i++;
  } else if (word === 'basically'){
     i++;
  }
})
console.log(i);

(Please excuse if the formatting come out really badly there ^^ I’m on mobile…)

2 Likes