Help with for...in Statement Challenge

Continuing the discussion from freeCodeCamp Challenge Guide: Iterate Through the Keys of an Object with a for…in Statement:

My code:

function countOnline(usersObj) {
  // Only change code below this line
for (let name in usersObj) {
  let newarr = [];

  if (name.online == true) {
    
    newarr.push(name);
  }
  
}
return newarr.length;
};

Although I see I have 'beaten about the bush' a little to get to the result. I don't understand why the code does not work.

I suggest you to play around logging all stuff out, like this:

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

function countOnline(usersObj) {
  // Only change code below this line
  
  
for (let name in usersObj) {
  let newarr = [];
  console.log('logging newarr... ', newarr);
  console.log('logging name... ', name);
  console.log('logging usersObj[name]...  ', usersObj[name]);
  console.log('logging name.online... ', name.online);
  console.log('------------');
  
  if (name.online === true) {
    
    newarr.push(name);
  }
  
}
return newarr;
};

console.log(countOnline(users));


/*
Output:

logging newarr...  []
logging name...  Alan
logging usersObj[name]...   { online: false }
logging name.online...  undefined
------------
logging newarr...  []
logging name...  Jeff
logging usersObj[name]...   { online: true }
logging name.online...  undefined
------------
logging newarr...  []
logging name...  Sarah
logging usersObj[name]...   { online: false }
logging name.online...  undefined
------------

HelloWorld.js:31
return newarr;
^

ReferenceError: newarr is not defined
*/

so from this we can see that newarr is not doing what it should do
and name.online is a problem

yes that is what I want to know - the problem with newarr
Also I tried changing name.online by using bracket notation but still the problem persists.

Thus the doubt.

the problem with newarr - you are setting it to an empty array every step of the loop.

But even placing it out of the for loop is not working.
I have tried that too as I thought this might have been the problem.

It’s the problem. But it’s not the only one.

The other one is this:

This accessing gives you nothing useful.
You should change name.online to something relevant

Once you figure this all out, think about this: do you need array here to accumulate result? You need to get a number. You can use just simple variable to gather result.

Also, try to log out some stuff like:

console.log(Object.values(usersObj));

That should give you additional ideas how to play around objects.