Using referenceless variables to access an object (Iterate Through the Keys of an Object with a for...in Statement)

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:

To put it another way, in the previous challenge we had the following:

'Alan' in users;

This returns true because there is in fact the property “Alan” in the object, users. But in the situation I describe above, there is no variable username in the object usersObj… there are just individual instances of users’ names. So when the function goes looking for username, how does it know when it has found an instance of it?

With the forin loop, you are populating the variable usernames with each of the usernames from the usersObj.

1 Like

When you put it that way, it makes sense why the function as a whole works the way it does. Thanks! But I have a hard time seeing how that “populating” activity emerges just from the bare code written in a for...in loop. Is the for...in loop “syntactic sugar” (to use a phrase I’ve read on here) for some more complex process, or should I be able to see why it “populates” the variable the way it does just from looking at the component parts of the syntax?

1 Like

the loop will give to the variable a different value at each iteration, this value being one of the properties name of the object

it is a loop, loops work by repeating the code inside them multiple times with different values

You can indeed use a for loop in place of a forin

// Counting function without for ... in
function countOnline(usersObj) {
  // Only change code below this line

  // Get keys
  let keys = Object.keys(usersObj);

  // Loop over keys
  for (let i = 0; i < keys.length; i++) {
    console.log("Key: " + keys[i])
    console.log(usersObj[keys[i]])
  }

  // Only change code above this line
}

// Setup object
let myUsersObj = {
  Alan: {
    online: false
  },
  Jeff: {
    online: true
  },
  Sarah: {
    online: false
  }
}

// Run function
countOnline(myUsersObj);