Basic Data Structures - Iterate Through the Keys of an Object with a for...in Statement

Tell us what’s happening:
Describe your issue in detail here.

Your code so far
I don’t know what is wrong here, can someone help me?

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

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

console.log(countOnline(users));

Your browser information:

User Agent is: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:109.0) Gecko/20100101 Firefox/113.0

Challenge: Basic Data Structures - Iterate Through the Keys of an Object with a for…in Statement

Link to the challenge:

What does return do inside a function? What about return inside a loop in a function?

Return statement inside a loop will break or stop the loop from executing any further and return a value(if provided any) and complete the function.
In your case you need to remove the return keyword from inside the loop to complete it properly, and return the “num” variable outside the loop.

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