For...in Statement

For...in Statement
0

#1

Tell us what’s happening:
I am trying to count the number of online users but it does not work.
Could you please help in finding the error in code?
Thanks…

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 count=0;
  if(user.online==true)
  count =count+1;
  return count;
}

  // change code above this line
}

console.log(countOnline(users));

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:62.0) Gecko/20100101 Firefox/62.0.

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


#2
for(let user in obj)
{
 let count=0;
  if(user.online==true)
  count =count+1;
  return count;
}

Each iteration you redeclare count and you reassign it the value 0.
I guess it return always 0 or 1 depending on user.online? ( i suggest to use === too btw)