Basic Data Structures: Iterate Through the Keys of an Object with a for...in Statement(may 2020)

Tell us what’s happening:

i don’t where i’m getting it wrong

Your code so far


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

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

Link to the challenge:

image

You changed code outside of

// Only change code below this line

and

// Only change code above this line

which has turned this code into a messy debugging tangle.

Why do you have two function defintions, one inside the other?

function countOnline(Obj) {
// Only change code below this line
function countOnline(obj) {

Why did you put the function call inside of the function?

console.log(countOnline(users));
}

i have made some corrections.
image

Please post the text of your code instead of screenshots. It makes it easier for me to run and help debug.

You are closer, but your code has let user in obj. What is obj? Where is it defined? What does it contain?

ok.

function countOnline(usersObj) {

  // Only change code below this line

let result = 0;

  for (let user in obj) {

    if (obj[user].online === true) {

      result++;

    }

  }

  return result;

  // Only change code above this line

}

Did you see these questions in my reply?

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

You misunderstand. users is not defined in your code. You only have userObj.

function countOnline(userObj) {
  ...
}
1 Like
function countOnline(usersObj) {

  // Only change code below this line

let result = 0;

  for (let users in obj) {

    if (obj[users].online === true) {

      result++;

    }

  }

  return result;

  // Only change code above this line

}

users is still not defined in your code. You only have userObj .

function countOnline(userObj) {
  ...
}

When you reference obj, you need to be instead using userObj, like is shown in the function signature.

1 Like

this is the original function signature.

function countOnline(usersObj) {
   ...
}

Correct. You are not using the userObj variable from the function signature in your code for the function body. You are instead using the undefined obj variable.

like more hint. please

function countOnline(usersObj) {
  // Only change code below this line
  let result = 0;
  for (let users in obj) {  // RIGHT HERE, THE VARIABLE obj IS NOT DEFINED
    if (obj[users].online === true) {  // RIGHT HERE, THE VARIABLE obj IS NOT DEFINED
      result++;
    }
  }
  return result;
  // Only change code above this line
}

The variable obj is never defined. Only the variable usersObj is defined. You need to use the variable usersObj.

there is no OBJ! there is only usersObj and you should use the usersObj object instead of just obj because it is undefined and there is only usersobj. Makes sense?

so whats your opinion?

I don’t understand what you are asking.

i’m still stuck. would appreciate any contribution

Is there something about the hint

The variable obj is never defined. Only the variable usersObj is defined. You need to use the variable usersObj .

that I can clarify? What part is unclear?