Need help with the for in loop questions

Tell us what’s happening:
Hey guys I’m unable to view the solution to this question for some reason it can’t find that webpage. Anyway my code looks right idk what I’m doing wrong.

Your code so far


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 num;
  for(let prop in obj) {
    if(prop.online === true) {
      num++;
    }
  }
  return num;
  // 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/70.0.3538.110 Safari/537.36.

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-data-structures/-iterate-through-the-keys-of-an-object-with-a-for---in-statement

two things:

  1. initialize num (as in let num = 0;) so you can do the num++ thing below.
  2. access the online property by using obj[prop].online === true.

here is what it should look like:

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

cheers!

Thank you:) Another question though.

I know the obj must be placed before the prop so we know which obj we are dealing with when accessing properties but in this case I went-
prop.online instead of-
obj[prop].online because i thought since the previous line of code was-
for(let prop in obj) has already stated that the prop or properties we are dealing with are coming out of obj?

The prop that you get is just a string e.g. “Alan”. It is a key of the object you are "for-in"ing. So it is not the object that you expected.

The only way to use it is by using bracket notation on the original object (obj).

I see, thanks again!