Mini Linter: step 4; *Word* of

I have completed the step 4 of the Mini Linter exercise ( https://www.codecademy.com/courses/introduction-to-javascript/projects/mini-linter ) with a help of the walkthrough video.

The code seems to be understandable for me but I am confused with how word of* works if we didn’t declare word before.

Here is the code:

let reallyCount = 0;
let veryCount = 0;
let basicallyCount = 0;

for (**word** of storyWords) {
  if (word === "really") {
    reallyCount += 1;
 		 }
  if (word === "very") {
    veryCount += 1;
		 }
  if (word == "basically"){
    basicallyCount += 1;
  }
}

console.log("really count: " + reallyCount);
console.log("very count: " + veryCount);
console.log("basically count: " + basicallyCount); 

It gives me the correct results:
really count: 2
very count: 5
basically count: 1

However, I don’t quite understand how word of* works. Is the word a sort of a keyword here?

word is declared in the for loop, python will now assign the items of the iterable (list in your case i assume) to the word variable.

Echoing what stetim94 said, word is a placeholder for each item in storyWords. So the program walks through the storyWords array, saves the current item in the word variable, and compares it according to your conditional.

Another tip: if you use else if then the program won’t have to check the other conditionals if it finds a match. For example, if you come across the word really in storyWords, the program as written will check it against the ‘very’ and ‘basically’ conditionals even if it found a match in the first one. If you use ‘else if’ it won’t bother with the subsequent conditionals if it finds a match. This won’t make a difference in the accuracy of your results, but saves some time and steps. If the storyWords array were very long, you might notice a difference.

Could you specify what you are referring to by python? It’s JavaScript.

Thank you. I tried adding the keyword “let” before in the following way: for (let word of storyWords)and I receive the same result: really count: 2
very count: 5
basically count: 1

I suppose that the instructor didn’t declare the variable “word”, is that correct? This is the walkthrough video: https://www.youtube.com/watch?v=0HtVAEgCvrE (you can check the video from 16:39 about the fourth step I am referring to.

Also, if I use “else if” instead of only “if” will it affect the time the page loads?

You can use for (let word of storyWords) or for (const word of storyWords) or for (var word of storyWords). Here’s a link to the documentation:

If you got the right result without declaring word with a keyword then I guess it doesn’t matter.

I think stetim94 just mistyped python instead of javascript; they may have been answering a python question before this one.

For this short of a passage you’re not going to see any difference between the performance of if ... if ... if as opposed to if ... else if ... else if the categories are mutually exclusive. It can cause some bad side effects if they’re not.

Like if you say:

if (x < 10) {do something};
if (x < 20) {do something else};

For x === 5 it will do something else, because it fulfills both conditions.

However, if you write
if (x < 10) {do something}
else if (x < 20) {do something else}

it will do something because the else keyword will only do something else if the first condition is not satisfied.

1 Like