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

Tell us what’s happening:

I feel embarrassed to even share my code at this point. I’ve tried so many things that I don’t even know what code has been the closest one to the solution. I just can’t figure this one out!

Seems so easy, at the same time so frustrating not to figure it out this far. What an impotence feeling. Please if someone could help, not just with the code but also with explaining the reason behind it. I would greatly appreciate that. Thanks

**The exercise asks **
We’ve defined a function, countOnline ; use a for…in statement within this function to loop through the users in the users object and return the number of users whose online property is set to true .

function countOnline(obj) {
// change code below this line

// change code above this line
}

console.log(countOnline(users));


**Your browser information:**

User Agent is: <code>Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.103 Safari/537.36</code>.

**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

This exercise tests your ability to navigate through an object literal & how to use for…in.

Normally., you would almost never use for…in. In fact, the solution for this example is a simple one liner., but since that has no educational value I will stay to the directions.

Directions:
use for…in
count users online

Well the first thing you can do visually is count the users who are online with your eyes. So the next question is how to navigate to it.

Let’s start with a different example:

const friends = {
    Shawn,
    Mike,
    Sally,
    Sue
};

If you wanted to cycle through each friend,. you could use for…in to do that by:

for (let name in friends){
    console.log(name);
}

In this example,. name is the dynamic variable. Meaning it changes on each iteration. friends remains the object literal.

In the example above you would output each friend.

Let’s take this example a step further…
Let’s say each of your friends had properties (or objects) that described different traits about each one.

const friends = {
    Shawn: {
        age: 21
    },
    Mike: {
        age: 25
    },
    Sally: {
        age: 28
    },
    Sue: {
        age: 39
    },
};

So now when we use this for…in

for (let name in friends){
    console.log(name);
}

we can use these names to cycle through all of the properties.

So to get Shawn’s age. You would do:
console.log(friends[‘Shawn’].age);

So if I told you I needed everyone whose ages were greater than 24., then you would go through each person and keep track of only the people who ages were > 24.

let counter = 0;
for (let name in friends){
    if (friends[name].age > 24){
        counter = counter + 1;
    }
}

the reason why name is in square brackets,. is because we use the value of name (which changes) and not the string value of ‘name’.

Everything I’ve explained in this should help you solve this problem. Good luck. :smiley:

1 Like

Your response has been very explanatory and useful, I want to thank you for taking the time. The main point I am not able to figure out is;

  1. The function countOnline returns the number of users with the online property set to true

what do I use to call out the number? for example: in your example you called out the name(s) by using ‘name’. I use ‘number’ to call out the numbers of users online and still no luck.

I tried using ‘number’ to call the number but it was undefined and I could not figure out how to define ‘number’ and effectively call out the number of users correctly.

Please know that I am very new to coding. Thank you for your patience. I am trying to keep up with patience and motivation.