Iterating through keys of an object with for..in statement

Tell us what’s happening:

Hey there,

Not sure if I missed something but having just looked at the code of other samples for this challenge, I’m wondering if the FCC starting code is set up right. Not sure if the code should start with a function (countOnline) or it should start with User definition. Also, the argument passed in the function, should it be usersObj or just users? Also checked on the FCC Github repo to check if the error was just me or it was from source and seems like it is from source.

tl;dr: Was getting bunch of errors then realised that start code given by FCC might have errors. Wanted to check.

Cheers

Your code so far


function countOnline(usersObj) {
// change code below this line

// change code above this line
}

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.130 Safari/537.36.

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

Link to the challenge:
https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/basic-data-structures/iterate-through-the-keys-of-an-object-with-a-for...in-statement

Welcome, thePCDguy.

Could you please explain what you did to get the errors you are?

The challenge says:

We’ve defined a function countOnline which accepts one argument (a users object).

I do not see any problems.

Go look at the hints again, it was a bit confusing to see that using the dot notation twice can cause errors. hint 2 is what you need to look at.

Array[element_in_array].Property_of_that_element

that is the syntax of the square bracket notation.

1 Like

Just read the statement again that @Sky020 quoted and it just clicked in my head. Typed in the code below and it worked.

Thank for the help. Guess looking at it in a different perspective helped out here!

function countOnline(usersObj) {
  // change code below this line
let count = 0;
for(let users in usersObj){
  if(usersObj[users].online===true){
    count++;
  }
}
return count++;
  // change code above this line
}