Tell us what’s happening:
I was able to solve the challenge with the code below, but what I don’t understand is what the variable username
is referring to in the function, or how it refers to anything in the object at all. I understand how the variable/parameter usersObj
works in the function, because you actually feed a reference for that into the function (i.e., the entire object). But it seems to me that username
is just a referenceless variable, or empty variable, if I can put it that way.
In other words, when the function goes looking for some instance of usersObj[username].online
, I see how it knows what to access with usersObj
, because that is a reference to the whole object passed into the parameter. And I see how online
is a clear reference to properties that in fact exist in the object. But how does the function know that any of the specific user names (e.g., Jeff, Alan, Sarah) count as instances of username
?
Your code so far
function countOnline(usersObj) {
// Only change code below this line
var a = 0;
for (let username in usersObj)
{if (usersObj[username].online == true) {a++}}
// Only change code above this line
return a;
}
console.log(countOnline({Alan: { online: true }, Jeff: { online: false }, Sarah: { online: true } }))
Your browser information:
User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.61 Safari/537.36
.
Challenge: Iterate Through the Keys of an Object with a for…in Statement
Link to the challenge: