Tearing my hair out. Then I used 'var' and all tests passed

Tell us what’s happening:
Describe your issue in detail here.
I spent how long trying to figure out why it would not pass the test ‘use a for…in loop’ .
I could see the ‘for…in’ loop right there on the screen.
Eventually I deleted the ‘key’ from the line above it that said: let c = 0, key;
and put ‘var’ in front of ‘key’ on the ‘for…in’ line.

THen it passed the tests. As far as I can tell, using ‘let’ is better than using ‘var’, and it does not have to be on the same line as the for…in.
What if I had not tried out of desperation, the ‘var’ thingy.
Very frustrating. Is there any way the test could check for ‘better practices’ as well.
Sorry to complain. You can ignore it or close it, I guess.

PS. I can’t put my code in here, because now it has passed all the tests, so the code is gone. Besides, if I put the code here (from the download), it would give the solution to all and sundry, then they won’t figure it out for themselves (besides the ‘var’ thing, that is).

  **Your code so far**

function countOnline(usersObj) {
// Only change code below this line
//I deleted 'key' in this . . . 
   let c = 0, key;
  for (key in usersObj) {
  };

//and put 'var' here . . .
  for(var key in usersObj) {
  };
// Only change code above this line
}
  **Your browser information:**

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/95.0.4638.69 Safari/537.36 Edg/95.0.1020.53

Challenge: Iterate Through the Keys of an Object with a for…in Statement

Link to the challenge:

Well the tests expect you to declare the variable within the loop-head, as shown in the example.
Reason being - it’s a variable specifically designed for the loop. You don’t need it outside and so you declare it inside. This way you don’t pollute the namespace with random variables OR run the risk of unexpected behavior because somewhere down the line you happen to write code using the word “key” again and suddenly it’s holding some random value from the last time it ran through a loop.

Also you can still use “let” - the problem wasn’t the word, but WHERE you put it. Only in the second example is it within the loop-head.

Ohhhh ok. Hadn’t thought of that. Sorry to get frustrated.
Thanks for the reply.

I am able to get it to pass with putting the let in the for loop.

It works for me, but does not pass if I declare the loop variable outside of the for loop.

Although I don’t know that it shouldn’t pass, but I definitely should be in the for loop. One huge advantage of that is that the for loop is scoped only to the for loop and the it doesn’t bleed into the outside scope. It is better practice.

It works exactly the same whether it is let or var. The issue (with the test) is where it is declared, not how.

But yeah, you should avoid var. Use const where you can and use let where you must (primitives that will need to change or reference types that need to be reassigned.)

1 Like

Ahah. Cool. Thanks for the advice.