-iterate-through-the-keys-of-an-object-with-a-forin-statement

Tell us what’s happening:
I’m trying to have a count variable keep track of how many of the users are on line. I suspect my bug is in my if (user.online) call, but I’ve tried every variation I can think of (using users.user.online, online, using brackets around the terms, and can’t get the code to see the online key.

What am I missing here?

Your code so far



let users = {
  Alan: {
    age: 27,
    online: false
  },
  Jeff: {
    age: 32,
    online: true
  },
  Sarah: {
    age: 48,
    online: false
  },
  Ryan: {
    age: 19,
    online: true
  }
};

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

console.log(countOnline(users));

Your browser information:

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

Link to the challenge:

Let’s first check the type of your user variable in the for-loop:

function countOnline(obj) {
  // change code below this line
  let count = 0;
  for (let user in users) {
      console.log(typeof user)
    if (user.online) {
      count++;
    }
  }
  return count;
  // change code above this line
}

If you run this function, you will find that your user variable has a type of string. So, if you use a for in loop to iterate over an object, you will get the property names back as strings. This is not what you want, because you need the actual object. Given that online is not a property of string, the if statement always results in undefined.

Your users.user.online idea is actually pretty close to the solution. The only problem is that you can’t use dot notation with variables.

Let’s suppose you have an object and the name of a property stored in a variable:

let alan = {
 age: 27,
 online: false
};

let myProperty = 'online';

If you’re trying to access the online property with alan.myProperty, it won’t work. This is because there is no property in the Alan object with the name myProperty. Somehow, the value of the variable needs to be used, instead of the name of the variable. This can be achieved with bracket notation: alan[myProperty].

So, back to your problem: you can get the actual user object by using bracket notation: users[user]. This will give you back an object instead of a string. Then, you can use dot notation to access the online key:

if (users[user].online) {
      count++;
    }
6 Likes

I thought I’d tried that combo. That’s what happens when you code while overtired.

Thank you @thomkhakai !

Your answer to this post also helped me! But I still don’t understand why I can’t access the online property this way:

function countOnline(obj) {
let c = 0;
for (let user in obj) {
    if (obj.user.online === true){
        c++;
}
}
return c;
}

edit:

obj would be the argument given ,that in this case, it is the users object. User would be each of the iteration from the object, which means, the first time would be Alan, then maybe Sarah, etc. And then to access the online property of that object.

This exercise

@thomkhakai thank you so much for that explanation, I was nearly there, just kept getting undefined back.

using the typeof method will be something I try in the future for my debugging, thanks so much!

Need help please, i am struggling to solve this problem.
What was the right answer.
This is my code

let result = 0;
for (let user in users) {
if (users[user].online) {
result++;
}
}
return result;

If you have a question about a specific challenge as it relates to your written code for that challenge, just click the Ask for Help button located on the challenge. It will create a new topic with all code you have written and include a link to the challenge also. You will still be able to ask any questions in the post before submitting it to the forum.

Thank you.

(Or anyway post the challenge link and your whole code, because if that is whole code, it can’t work as you need to create a function)

Thanks,
i have just reposted the challenge