Dot notation error (for ...in)

Tell us what’s happening:
Describe your issue in detail here.
I have already passed this challenge so my question is not about my code but about the hints given. Why does dot notation fail here?

  **Your code so far**

function countOnline(usersObj) {
// Only change code below this line

// Only change code above this line
}
  **Your browser information:**

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.159 Safari/537.36

Challenge: Iterate Through the Keys of an Object with a for…in Statement

Link to the challenge:

Can you show the failing code?

I did not try it myself but the hint for the challenge was saying that the following code would not work :

for (let user in obj) {
  if (obj.user.online === true) {
    //code
  }
}
1 Like

Imagine that username is a string “john”.
obj.john.online ← will work nicely
Now imagine that username is a string “john jr”
obj.john jr.online ← this will throw an error because of a space.
JS will try to look for obj.john and then for jr.online.

1 Like

This works if obj has the property user, but it doesn’t. You want to use the string contained in user not use itself.

Say we have a user==="John". obj.user is looking for a property called user, butobj[user]===obj["John"] which is what we want.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.