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

Tell us what’s happening:
I am completely brain-dead :pensive: and desperately need your guidance.

Your code so far

const users = {
  Alan: {
    online: false
  },
  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 === false) {
    }
  }
  return num++

  // 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/116.0.0.0 Safari/537.36

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

Link to the challenge:

I spot a few issues here

For here you have this if statement but you are not doing anything with it because it is empty

and then here it looks like you are trying to force the return of one

Here is how I would suggest cleaning up your code

The logic of the problem is that you want to go through and see how many users in that list are online

For your if statement here, it would be more effective to check if users are online instead of if they aren’t

then inside that if statement, you can increment the count

then you can return the total number of users online

By revising your approach and logic, then you will get t the test to pass

hope that helps

2 Likes

Thank you very much. I had accidently deleted the lines of code earlier.
It is working now :sweat_smile: :+1:

  let count = 0;
  for (let checkOnline in allUsers) {
    if (allUsers[checkOnline].online === true) { 
      count++;
    }
  }
  return count;
1 Like