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

Tell us what’s happening:
Having difficulty in solving Last two parts of this please help me.

Your code so far

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

function countOnline(Users) {
  // Only change code below this line
let result = 0;
  for (let user in users) {
    if (users[user].online === true) {
      result++;
    }
  }
  return result;
  // 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/114.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:

Why did you change this line? You shouldn’t change this line.

You shouldn’t use the global variable here.

can you help me by writing code coz it’s still not working in that way

No, I cannot write the answers for you.

What changes did you make to fix the two problems I pointed out?

Your iterating or looping over the the object passed into the function so why would you create a random object in the for...in loop that doesn’t exist? Yes users exists as a global object but you need to use the local object passed into the function.

syntax:

// Here the `variableIMadeUp` changes to a different value in each step (iteration)
for(const variableIMadeUp in objectImLoopingOver) {
console.log(varibleIMadeUp);
}

objectImLoopingOver needs to be the same name as the parameter of the function.

function someFunction(objectImLoopingOver) {
    // my for...in loop goes here
}


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