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 result = 0;
for (let user in obj) {
if (obj[user].online === true) {
result++;
}
}
return result;
// change code above this line
}
console.log(countOnline(users));
So I tried this code as a solution but it doesn’t seem to work. So I googled it and I think I found the mistake. The increment sign (++) is on the wrong side of the variable result.
So I think the correct solution would be to put ++result instead of result++.
Your code has been blurred out to avoid spoiling a full working solution for other campers who may not yet want to see a complete solution. In the future, if you post a full passing solution to a challenge and have questions about it, please surround it with [spoiler] and [/spoiler] tags on the line above and below your solution code.
Hey @ILM,
I have a question related to this project, but may be more about my syntax in general.
My answer:
function countOnline(usersObj) {
// Only change code below this line
let result = 0;
for (let user in usersObj) {
if (usersObj[user][online] == true) {
result++
}
}
return result;
// Only change code above this line
}
the actual solution :
function countOnline(usersObj) {
// Only change code below this line
let result = 0;
for (let user in usersObj) {
if (usersObj[user].online == true) {
result++
}
}
return result;
// Only change code above this line
}
The only difference between what I wrote and the solution:
Mine:
if (usersObj[user][online] == true)
Solution:
if (usersObj[user].online == true)
Why does dot notation work, but not bracket notation?
if you use online like that inside bracket notation, it’s a variable, to use bracket notation you need to write a string literal, "online", with dot notation it works because it accesses properties with the exact name stated
Thank you! This makes total sense; I recall this rule from the objects sections at the end of basic JS and regex sections.
Also, I recognize you’re one of the most active leaders in on the sections I’ve been learning. Just want to recognize this and say thank you for your overall presence and willingness to answer questions. Cheers
let users = {
Alan: {
online: true
},
Jeff: {
online: true
},
Sarah: {
online: false
}
}
function countOnline(usersObj) {
// Only change code below this line
let count = 0
for ( let user in usersObj){
if(usersObj[user].online == true){
count++
}
}
return count
// Only change code above this line
}
console.log(countOnline(users))
//My advice would be to always use console.log() to check your outputs.
For Instance:
for ( let user in usersObj){
console.log(user)
} output will be [Alan, Jeff, Sarah]
We are trying to cut back on the number of spoiler solutions found on the forum and instead focus on helping other campers with their questions and definitely not posting full working solutions.
You can post solutions that invite discussion (like asking how the solution works, or asking about certain parts of the solution). But please don’t just post your solution for the sake of sharing it.
If you post a full passing solution to a challenge and have questions about it, please surround it with [spoiler] and [/spoiler] tags on the line above and below your solution code.