Iterate Through the Keys of an Object with a Statement - return vs. console.log etc


Below is my solution for this challenge.
But I have some questions.

  1. Instead of return, I used console.log(counter).
    Result has to be “2”, but if I use console.log, it was “2, undefined”.
    Why is it so??
  2. I understand “user” is variable that has to be in the bracket.
    and “online” is specific root that you can use both bracket and dot notation.
    but how about “obj”? I try using bracket and it failed to read. Why is it so?
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 counter =0;
  for (let user in obj){
  } return counter;
  // change code above this line
  1. When you replace the return by the console.log, the first value will be the counter printed, the second value ‘undefined’ is printed from the console.log(countOnline(users));. Since the function call doesn’t return anything, the value from countOnline(users) is undefined.

  2. Do you mean [obj][user] instead of obj[user]? That is because obj is the object that contains the data. In obj[user], user is a variable containing a key value of obj you are looking for.

Thank you for clear and the fast reply! :slight_smile: