Iterate Through the Keys of an Object with a for...in Statement challenge

Hi guys why is my code passing??

  **Your code so far**

const users = {
Alan: {
  online: false
},
Jeff: {
  online: true
},
Sarah: {
  online: false
}
}

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

console.log(countOnline(users));










  **Your browser information:**

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:97.0) Gecko/20100101 Firefox/97.0

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

Link to the challenge:

JavaScript is case sensitive - try to find your typo!

1 Like

thank you found it!!

why is it that when i remove the result++ the test is no longer passing?

if you remove it, what is going to count the number of people online?

oh i get it now.then lastly why this

let result = 0;

because you can’t add 1 to a number that doesn’t exist?

I don’t understadn what you are asking

let result = 0;
for (let user in usersobj) {
if (usersobj[user].online === true) {
  result++;
}
let result = 0; before the for in statement.

what’s your question about that code?

How it came about. I applied it while I was reading the tread on fcc forum, while I was trying to solve the challenge. but didn’t know what the code actually did on the challenge

haven’t you worked it out on your own?

Hi @columbuschidozie1 !

To understand what the code is doing we first have to break down the problem in plain english.

Our goal is to find out how many users are currently online at the moment.
If you look at the list of users here you can see we only have one user online which is Jeff.

const users = {
  Alan: {
    online: false
  },
  Jeff: {
    online: true
  },
  Sarah: {
    online: false
  }
}

Your goal of your code is to go through the list of users and identify which ones have online:true

The result represents the number of users online that is why were are returning that here

 return result;

We use a for in loop to loop through the object.

 for (let user in usersObj)

Our if statements checks if the user is online.

if (usersObj[user].online)

Hope that makes sense!

on the solution how did this come about let result = 0;

Result represents the number of people currently online.
Before you loop through the object it needs to be initialized to 0 since we haven’t checked yet if anyone is online.

oh fantastic.whats wrong with this code?? kept getting reference error let result = 0;

for (let user in usersObj) {

  if (usersObj[user].online === true) {

     result++;

  }

}

return result;

can you copy the whole function and paste it in the reply?
right now, I am not seeing the declaration for result in your current code here

function countOnline(usersObj) {

let result = 0;

for (let user in usersObj) {

  if (usersObj[user].online === true) {

     result++;

  }

}

return result;

}

I am not getting a reference error with this new code you posted

Also as a side note, you don’t need to explicitly say === true because this usersObj[user].online will either be true or false in the if statement.

You can just write this

if (usersObj[user].online) {

     result++;

  }

still getting the same error