Iterate Through the Keys of an Object with a for...in Statement, why this isn't work

Tell us what’s happening:
Describe your issue in detail here.

  **Your code so far**

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

function countOnline(usersObj) {
// Only change code below this line
let number = 0
for(let u in usersObj){
  if(users[u].online == true){    **//it not work**
    number++
  }
}
return number
// Only change code above this line
}

console.log(countOnline(users));

///////////////////////////////////////////////////////////////////

function countOnline(usersObj) {
// Only change code below this line
let number = 0
for(let u in usersObj){
  if(usersObj[u].online == true){   **//it work**
    number++
  }
}
return number
// Only 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/99.0.4844.74 Safari/537.36

Challenge: Iterate Through the Keys of an Object with a for…in Statement

Link to the challenge:

The one that doesn’t work uses the hard coded global object. You want to use the one that is passed in so you can use this function with any object. I’m assuming that the test is passing in different objects to test.

1 Like