Tell us what’s happening:
I’m really not trying to look at the solution anymore until i figure out the problem myself. I created the sum variable so that the boolean ‘true’ can be converted to a number. Can anyone help give me some guidance as to what I might be doing wrong. Thank you!
Your code so far
function countOnline(usersObj) {
// Only change code below this line
var sum = 0
for (let online in usersObj)
if (online === true)
{return sum = sum + 1}
else
return 0
// Only change code above this line
}
Your browser information:
User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.121 Safari/537.36.
Challenge: Iterate Through the Keys of an Object with a for…in Statement
function countOnline(usersObj) {
// Only change code below this line
var sum = 0
for (let online in usersObj)
if (online === true)
{sum = sum + 1}
else
return sum
// Only change code above this line
}
No, there is no requirement to use an else or else if statement with an if statement. You only use the logic syntax needed. Nothing more. Nothing less.
function countOnline(usersObj) {
// Only change code below this line
var sum = 0
for (let online in usersObj)
{if (online === true)
{sum = sum + 1}
}return sum
// Only change code above this line
}
im so close but its still not working. sorry for asking so many questions im just not sure how else to trouble shoot this and want to make progress
function countOnline(usersObj) {
// Only change code below this line
var sum = 0
for (let online in usersObj) {
console.log(online); // what is online?
if (online === true) {
sum = sum + 1
}
}
return sum
// Only change code above this line
}
// test the function
countOnline({ Alan: { online: true }, Jeff: { online: false }, Sarah: { online: true } });
Does adding this console.log help you see what’s going on here? You need to do something more complicated with this variable.