Iterate Through the Keys of an Object

Iterate Through the Keys of an Object
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
  for(let obj1 in obj){
    if(obj1.online == true)
     return obj1;
  }
  // 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

I am not knowing where I had gone wrong. Anyone out there who could fix this for me ?


#2

You have 3 problems with your code above.

  1. You should be using { } to surround your if statement code block. The test suite will have issues with you not using them.

  2. You return within the first iteration of the for loop. When a return statement is executed, the function is immediately exited and does not return to finish a loop.

  3. Think about what value you are returning in this in the return statement. The challenge wants you to return a number representing the total number of users with an online property value of true. You need to think about how you are going to calculate this inside the for loop, then only return the count once the for loop has completed.


#3

still I cannot gather where bug lies


#4

What changes did you make to your code?