Stuck with `for...in`. please help!

What’s the last code you tried? We can keep helping you working on it

Let’s start with the original code:

for (let user in obj) {
  return user.online;
}

The user variable is just a key, and it doesn’t make sense to get the online prop. Also, it doesn’t make sense to return just then.

First hint (click to reveal):

To access the actual user, you need to use users[user]. It actually doesn’t make sense to name it user, maybe name it key.

What this does is it looks at the object, and finds the value with the key of the value of the user or key variable.

Next hint:

Create a count variable at the beginning of the function with the value of 0. Then, every time you find an online user, increase that count.

After that, you should be able to figure out what to do with that :wink:

No, it should be with bracket notation.

I got this:

var numUsersOnline = 0;
for (let user in obj) {
  var a = users[user.online];

if(a == true){
  numUsersOnline ++;
}

}
return numUsersOnline;

but I don’t understand why it only returns zero.

You’re close! By the way, == true is not needed. The problem is here:
users[user.online]

Look very carefully. A string does not have the online prop.

I got this:

var numUsersOnline = 0;
for (let user in obj) {
  var a = obj[user];

if(a){
  numUsersOnline ++;
}

}
return numUsersOnline;

got it right, but the challenge wouldn’t let me pass.

Nope, still not quite right. You’re not checking if the user is online:

obj[user]

On that, access the online proprty.

1 Like

I’m just commenting to follow this. I solved it before, but even after looking at all the comments I can’t figure it out now.

Of course it’s easy if you know the answer. But I finally got it.

Thank you so much! I finally solved it!!
So far, you gave the most help.

1 Like