Iterate Through the Keys of an Object with a for...in Statement

@cjbechtl I believe your basic code is ok, therefore this is a bug.

This was the answer. Thanks @chinonsobere.

This is the answer.
function countOnline(obj) {
// change code below this line
let count = 0;
for(let i in obj){
if(obj[i].online === true) {
count++;
}
}
return count;
// change code above this line
}

Counter is used to store the value of the function. we need to keep the count of online users so for example
let counter = 0; //initial value of counter is 0
for(let x in obj) {
if( x.online === true) // if the condition is true ie. if the value of online is true it will increment counter by //1 ie for now one user is online
counter++;

second time the function runs again if the above condition is true it will again increment the counter.
In this way we can keep track of the users those are online

1 Like

This was the tip that finally helped me get over the hump.

let users = {
  Alan: {
    age: 27,
    online: false
  },
  Jeff: {
    age: 32,
    online: true
  },
  Sarah: {
    age: 48,
    online: false
  },
  Ryan: {
    age: 19,
    online: true
  }
};

function countOnline(obj) {
  // change code below this line
  let count = 0;

  for (let user in obj) {
    (obj[user].online == true) ? count++ : 0;
  }

  return count;
  // change code above this line
}

console.log(countOnline(users));

It worked.

1 Like

Thank you for this simple and effective explanation.

i figured it out guys. actually to access the variable ‘user’ u gotta use bracket notation without quotes.

tip >>> obj[user].online

It seems that using the ‘obj’ argument as a function variable does not invoke the value passed into it, in this case ‘users’, as you would think. If someone could explain why that is would be helpful. The way it is working means that the function is useless with any other array name.

But the ‘obj’ parameter would not work when substituted.

Is the user considered a string in the OP’s code because it is a variable, or because it is referencing an object property?

the for... in loop gives strings as value of the iterated variable, and the values are those of the object keys. user is a variable with value of a string

I’m going to complicate this entire thread by telling you, never to use for…in and always use Object.entries instead.

well, until you don’t suggest that to pass this challenge… I doubt it would work

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

I’ve found this way works fine using bracket and dot notation.

1 Like

Thank you. The variable cannot be used in dot notation.

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

Adding my own 2 cents… :slightly_smiling_face:

<redacted>

I don’t think you need the comparison checker |"==" or “===” as the if statement will on execute if true

It is great that you solved the challenge, but instead of posting your full working solution, it is best to stay focused on answering the original poster’s question(s) and help guide them with hints and suggestions to solve their own issues with the challenge.

If you want to compare your solution to others, use the Get a hint button on the challenge and there are alternative solutions you can compare yours to. Also, you can probably search older posts using the forum search feature or google the challenge name and find more there.

We are trying to cut back on the number of spoiler solutions found on the forum and instead focus on helping other campers with their questions and definitely not posting full working solutions.

Thank you for understanding.

1 Like

That’s clear, Thanks for the heads up…