For...in Statement Iteration

Tell us what’s happening:

Can some one please help me out with this I’ve been stuck for two days after checking the web I still can’t figure out what I have wrong

Your code so far


let users = {
  Alan: {
    age: 27,
    online: false
  },
  Jeff: {
    age: 32,
    online: true
  },
  Sarah: {
    age: 48,
    online: false
  },
  Ryan: {
    age: 19,
    online: true
  }
};

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

for (let user in obj) {
  
 let userNumber = 0;
  if (obj[user]["online"] === true) {
       
        userNumber++;
      }
       console.log(userNumber);
 
  }
 
  // change code above this line
}

console.log(countOnline(users));

Your browser information:

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

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

You’ll want to declare the userNumber variable before starting the for-loop. Otherwise it will just reset to 0 at each loop.

Your function also isn’t returning a value.

2 Likes

thanks for the help I finally figured it out