How to return how many time true occurs

so I am at this challenge and this is the way I found to count how many times true occurs, but how can I save the result if it resets after every iterate?

  **Your code so far**

const users = {
Alan: {
  online: false
},
Jeff: {
  online: true
},
Sarah: {
  online: false
}
}

function countOnline(usersObj) {
// Only change code below this line
for (let user in usersObj) {
if (usersObj[user].online === true) {
let i = 0
i++
return i;
}
}
// Only change code above this line
}

console.log(countOnline(users));
  **Your browser information:**

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

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

Link to the challenge:

Hi, your code is correct but the way you positioned somethings are wrong.

for (let user in usersObj) {
if (usersObj[user].online === true) {
let i = 0
i++
return i;
}

What this does is , set i to 0 each time ,increment by one if true and return 1.

To fix this, move let i=0 to the place above the if statement and below the foor loop.
And also move the return i to just above the comment.
Leave the i++ as it is.

Now your statement defines let i=0, which sets i to 0. If true, it adds one to i each time. After the loop executes, it returns the value of i.

Hope this helps :smiley:

1 Like

ty but, moving I above for loop fixed it a long the other stuff too.

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