Could anyone explain why usersObj.users.online === true doesn’t work, but usersObj[users].online does, even though both options are in solution hints. What’s the deal here ?
function countOnline(usersObj) {
let count = 0;
// Only change code below this line
for(let user in usersObj) {
if(usersObj[user].online === true) {
count++;
}
}
return count;
// Only change code above this line
}
Challenge: Iterate Through the Keys of an Object with a for…in Statement
const users = {OP:'ViyanMd', reply:'santimir'}
for (let user in users){
let val = users[user]; console.log(val)
}
for (let user in users){
let val = users.user; console.log(val)
}
Square bracket notation allows access to properties containing special characters and the selection of properties using variables.
Using dot notation you can access properties that do not contain special characters or variables.
I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.
You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.