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

How Can I? Iterate Through the Keys of an Object with a for...in Statement
0

#1

Tell us what’s happening:
My code logs each instance of the condition being true, but I don’t know how to mutate this information to return a sum…

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 prop = 'online'
for (let x in obj) {
  if (obj[x][prop] === true){
     console.log(obj[x][prop])
  }
}
  // change code above this line
}

console.log(countOnline(users));
//Returns true true

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

You need to create an accumulator and increment it where you are currently logging to the console.