Iterate Through the Keys of an Object with a for...in Statement?

Tell us what’s happening:
I don’t really know how to increment the i so that it can be returned. Any hints or links to another forum.
i know i cant have i = 0 outside of the for function because then it will just return 0 now matter what.

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
var i: 0;
for (let user in users) 
  if(console.log(users.online) == true){
    i++;
    return i;
  };


  // 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/71.0.3578.80 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/

If you return something inside the loop you break the loop, instead you would need to use return after the loop

var i: 0 you need to use the assign operator, so var i = 0

ok i understand that i should put the return i outside the loop but if I make var i = 0 then when it goes to return i it will always be 0 since that is what it is assigned to. Should put var i= 0 inside the loop somewhere so that it doesn’t stay at 0?

You set the starting value of i at 0. Then variables can be manipulated. Each time i++ is eseguited the value of i changes. If you use the return statement inside the loop the possible highest value is 1, because return stop the loop from running. If you put return outside of the loop then it will return the value of the variable after the loop was run all the needed time.

If you assign i = 0 inside the loop each time the loop run i is set to 0, and so the higher it can get is 1.