Can't iterate through the keys of an Object

I’m not sure why my test is returning 0 - it should return 1. Must be doing something wrong, though I don’t know what. I’d appreciate help please.

  **My code so far**

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

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

console.log(countOnline(users)) // this returns 0 instead of 1
```js


      **Your browser information:**

User Agent is: <code>Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.212 Safari/537.36</code>.

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

**Link to the challenge:**
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

You have a return inside of a loop. Your code immediately stop and exits the function the very first time it encounters a return statement.

2 Likes

Oh yeah, this was it. Thank you so much, really appreciate it.

1 Like

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