Why is my countOnline function not working I don't get it

Why is my countOnline function not working I don't get it
0

#1

Tell us what’s happening:

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
     let count = 0;
    
    for(let user in obj){
      if(obj[user].online === true){
          count++;
      }
        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) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 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


#2

The problem here is with the return statement.
When the interpreter finds the word return it returns the value and stops.

The same goes for return in a loop statement,
It runs only once => finds return => stops.

That’s not what it is wanted.

You want to return the value of count only after the for…in loop finishes looping the obj.

Fix that.

And also, do not forget to take off theconsole.log from the function call countOnline(users);


#3

Thanks a million, my return statement was still in the for loop.


#4

You’re welcome.

Good Luck & Happy Coding!