Nice to meet everyone. First time posting on here. I’m having troubles figuring out how to return a count total of whenever a user is “online”. My code looks like it detects that first “true” value and ends without iterating through the whole object. Looks like I might be overthinking things haha. Any help or guidance would be appreciated.
Thanks in advance!
**Your code so far**
function countOnline(usersObj) {
// Only change code below this line
let count = 0;
for (let prop in usersObj) {
while (usersObj[prop].online === true) {
count++;
return count;
}
}
}
console.log(countOnline({ Alan: { online: true }, Jeff: { online: true }, Sarah: { online: false } }))
**Your browser information:**
User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/93.0.4577.82 Safari/537.36
Challenge: Iterate Through the Keys of an Object with a for…in Statement
I was able to pass with your code when I made those changes.
Can you post your updated code here?
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.
I forgot to add the return outside of the for loop! It works and makes sense on why it should be outside the loop. Thanks so much for clearing that up.