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

Tell us what’s happening:

Describe your issue in detail here.
This code must also be right by the logic of the lesson. Please suggest why the logic is wrong.

Your code so far

const users = {
  Alan: {
    online: false
  },
  Jeff: {
    online: true
  },
  Sarah: {
    online: false
  }
}

function countOnline(allUsers) {
  // Only change code below this line
  let count = 0;
  for (const user in allUsers) {
    for (const online in user) {
      if (user[online] == true) {
        count++;
      }
    }
  }
  return count;
  // Only change code above this line
}

console.log(countOnline(users));

Your browser information:

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

Challenge Information:

Basic Data Structures - Iterate Through the Keys of an Object with a for…in Statement

You appear to have created this post without editing the template. Please edit your post to Tell us what’s happening in your own words.

I don’t understand why this code’s logic is wrong and want to why it won’t work although I understood the correct code as well.

What does this do? I don’t think it does what you think

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.