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

Tell us what’s happening:

This is the message I am getting: The function countOnline returns the number of users with the online property set to true

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 obj) {
    if (user.online === true) {
    user++;
    count.push(user);
    }
    return count;
  }
  // change code above this line
}

console.log(countOnline(users));

Your browser information:

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

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

Take a close look at what you are incrementing here. I think you meant to use variable count.

*Hi @shimphillip! Okay, so I tried changing user++ to count++ instead but still getting the same error message…

Yes, there are 2 more wrong things here.

First you need to have a correct conditional in your if statement. user.online isn’t giving anything. You need to retrieve its value then access .online.

Next, don’t return count too early. Make sure you return it after the loop.

2 Likes

Hi @LauraMeeHan!

That was a very good attempt at the problem. What I think you are looking for is of, instead of in. I had this problem many times, personally. let user in obj means that user will be an index not object. Thus when you say user.online what you are actually doing is 5.online which of course can’t happen. Here is a refrence for in, change in to of and you get your desired outcome, but you might need to correct your checks :stuck_out_tongue: but I think you are heading the right direction!

Ah on the note if you plan to use in instead of, remember to use the array notation [] such as obj[user], thus you can do something like obj[user].online === true check. But, using in (user is an number of the current position of the user in the array and not an actual user) means you are getting an index not the object in general :slight_smile: but a tip non the less for the problem.

Hope it helps :blush:

5 Likes

Thank you @Hazecode for all of your help! I’m going to try to use the in instead of of but I noticed I do tend to forget the array notation [] so thank you for that tip! Will definitely have to get better at that. And thank you for the MDN references, I appreciate it! I’ve never used the for of before so I learned something new! :grinning:

1 Like

Fantastic, the mdn is a great place to go for quick reminders and checks.

Glad to help, keep going mate!

Oh my goodness, that was it!! :smile: Yay! I was for sure putting my return too early and it worked once I moved it further down. I also made the noted change in my conditional to include the [] so this part looks like this now:
if (obj[user].online == true) { count++; Thanks friend!

3 Likes

another option
function countOnline(obj) {
let a = 0;
for (let user in obj) {
if (obj[user].online == true) {
a+1;
}
}
return a;
}

This doesn’t work, after everything a is still 0 - maybe you want to review how to change the value of a variable

Thank you so much, bro, you save my time.

Thanks everyone for all the helpful replies in the thread, I managed to get my code to pass the tests.

However, I was still wondering why, when we use bracket notation for the if statement (e.g. [obj][user][‘online’]. that we only put the last part (online) into quotation marks?

I was under the (incorrect) impression that we would need to use quotation marks for any keys/values we were using bracket notation with.

I suggest going back over the following three challenges which discusses when to use each.

Accessing Object Properties with Dot Notation

Accessing Object Properties with Bracket Notation

Accessing Object Properties with Variables

1 Like

Thanks for the quick response Randall,
I think I understand now, we only use quotation marks when referring explicitly to a property in an object.
Both ‘obj’ and ‘user’ were variables in the function and therefore don’t need the quotation marks.

The reason you would not use [obj] is because obj is the actual object. It is a variable, but it is not a property. It is really the fact the user is a variable and represents a property of an object you want to access.