Basic Data Structures: Iterate Through the Keys of an Object with a for...in Statement question

Hello. This is my code:

function countOnline(usersObj) {
  // Only change code below this line
for (let user in usersObj) {
 
  console.log(user);

  for (let online in user) {
  console.log(online);
}
}
  // Only change code above this line
} 

But nothing happens. Any ideas please?

Hello, could you please post a link to the challenge you are working on so that people can help you more easily?

What are you trying to accomplish? What isn’t working? What else have you tried?

Here is the link: https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/basic-data-structures/iterate-through-the-keys-of-an-object-with-a-for…in-statement .

I am trying to iterate through the keys and subkeys of the object.

If you have an object of objects, then your current code will print the key for each object in usersObj and followed by the keys in that object. Is that what you are trying to do?

Yes, but my code doesn’t have that effect.

Sorry. I misread before. You’re doing for(let online in user) you’ll just be printing the index of every letter in the key. I thought you had for(let online in useraObj[user]).

Here is my code now, but still nothing is printed in the console:

function countOnline(usersObj) {
  // Only change code below this line
for (let user in usersObj[user]) {
 
  console.log(user);

  for (let online in usersObj[user]) {
  console.log(online);
}
}
  // Only change code above this line
}            

What do you think this loop is doing?

How can you loop over user in usersObj[user] ? What do you think this should be doing?

I really am not sure what is going on in this code. The link you provided to the challenge is broken, so I’m not sure what the challenge is. Is this the challenge where you loop over an array of users and count how many are online?

It’s this: Basic Data Structures: Iterate Through the Keys of an Object with a for…in Statement.

OK, so now I have this:

function countOnline(usersObj) {
  // Only change code below this line
for (let user in usersObj) {
 
  console.log(user);

  for (let online in usersObj[user]) {
  console.log(online);
}
}
  // Only change code above this line
}         

Why console.log prints nothing?

If you aren’t calling the function (which you aren’t in the code you’ve shown) then nothing will happen. Do you have an example where you called the code?

Here is the solution:
REDACTED
If you need any help understanding, let me know.

It is great that you solved the challenge, but instead of posting your full working solution, it is best to stay focused on answering the original poster’s question(s) and help guide them with hints and suggestions to solve their own issues with the challenge.

We are trying to cut back on the number of spoiler solutions found on the forum and instead focus on helping other campers with their questions and definitely not posting full working solutions.

Thank you for understanding.

My bad, thanks for letting me know.

Thanks for your input. Here is my code now:

function countOnline(usersObj) {
  // Only change code below this line

let count = 0;

for (let user in usersObj) {
 
  console.log(user);


  for (let online in usersObj[user]) {
  console.log(online);

if(online === true) {
  count = count + 1;
}

}

return count;

}
  // Only change code above this line
}                                                      
 
var usersObj = {
  Alan: {
    online: false
  },
  Jeff: {
    online: true
  },
  Sarah: {
    online: false
  }
}

countOnline(usersObj);

Here are my errors:

// running tests The function

countOnline

should return

1

when the object

{ Alan: { online: false }, Jeff: { online: true }, Sarah: { online: false } }

is passed to it The function

countOnline

should return

2

when the object

{ Alan: { online: true }, Jeff: { online: false }, Sarah: { online: true } }

is passed to it // tests completed // console output Alan online Alan online Alan online Alan online

Some help please? I can’t see what I’ve been doing wrong.

You aren’t accessing the online property of a specific usersObj[user] . The for loop syntax is not how you access a property. Why not try dot or bracket notation.

1 Like

So, here is what I have now:

function countOnline(usersObj) {
  // Only change code below this line

let count = 0;

for (let user in usersObj) {
 
  console.log(user);


  for (let online in usersObj[user]) {
  console.log(online);

if(usersObj[user][online] === true) {
  count = count + 1;
}

console.log(count);

return count;

}
  // Only change code above this line
}

}
 
var usersObj = {
  Alan: {
    online: false
  },
  Jeff: {
    online: true
  },
  Sarah: {
    online: false
  }
}

countOnline(usersObj);

But still I get errors:

// running tests The function

countOnline

should return

1

when the object

{ Alan: { online: false }, Jeff: { online: true }, Sarah: { online: false } }

is passed to it The function

countOnline

should return

2

when the object

{ Alan: { online: true }, Jeff: { online: false }, Sarah: { online: true } }

is passed to it // tests completed // console output Alan online 0 Alan online 0 Alan online 1 Alan online 0