I don't understand "Basic Data Structures: Iterate Through the Keys of an Object with a for...in Statement"

Tell us what’s happening:
I have no idea what to do in this challenge.

Your code so far


function countOnline(usersObj) {
// Only change code below this line
for (let user in usersObj) {
if (usersObj[user] === true) {
console.log(usersObj++);
return usersObj += 1;
} else {
  console.log(usersObj += 0);
  return usersObj += 0;
}

}

// Only change code above this line
}

console.log(countOnline({ Alan: { online: false }, Jeff: { online: true }, Sarah: { online: false } }));


Your browser information:

User Agent is: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:84.0) Gecko/20100101 Firefox/84.0.

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

Link to the challenge:

The goal is to return how many users inside the usersObj have the online property set to true. Does this sound more clear? How do you understand the task?

1 Like

My code is wrong, how do I fix it.

function countOnline(usersObj) {
  // Only change code below this line
for (let user in usersObj) {
  if (user.online === true) {
  
   usersObj += 1;
  } else if (user.online === false) {
 
  usersObj += 0;
  } else {
return undefined;
  }
}

  // Only change code above this line
}

console.log(countOnline({ Alan: { online: false }, Jeff: { online: true }, Sarah: { online: false } }));


Your first entry which used usersObj[user] in the for-in loop was close but you’re not drilling down deep enough. The question is how do you get the ‘online’ boolean value.

Try this function to console what usersObj[user] returns. If it’s not returning a true or false value then you have to access the property in that object that has a true/false value. How do you do that with objects?

function countOnline(usersObj) {
  for (let user in usersObj) {
    console.log(usersObj[user] ) 
  }
}
1 Like

When having this in the console

usersObj[user].online

the results shows

false
true
false
undefined

Exactly. So those are the values you need to check in your for-in loop. If it’s true then the person is ‘online’. The goal is to find out how many users are ‘online’.

1 Like

How do I count how many are online?

This is what I tried:

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

  // Only change code above this line
}

console.log(countOnline({ Alan: { online: false }, Jeff: { online: true }, Sarah: { online: false } }));


two things - is usersObj what you are using to store the number of online users?
and a return statement when executed will stop the function and return a value

1 Like

I don’t know what else to have.

do you understand the goal of this function?

1 Like

Yes, it’s to count how many is online.

how do you count something?

1 Like

I did += it’s how you count I think.

With a loop, it needs to repeat and count all.

and to what do you add the number?

1 Like

I would think countOnline.

I added it to usersObj, because I think it goes then to countOnline.

are you sure? remember that countOnline is a function

1 Like

I don’t know how else I’m suppose to add the online ones.

I have tried many things, nothing works.

how would you do that manually? would you keep track overwriting the object you are analizing?

1 Like

I don’t have anywhere else to add the numbers. They only give me user, usersObj and online

yes, that’s the starting point, and you need to write the logic to reach the end goal, including any new thing you need

1 Like