Strange logs in Basic Data Structures: Iterate Through the Keys of an Object with a for...in Statement

Strange logs in Basic Data Structures: Iterate Through the Keys of an Object with a for...in Statement
0

#1

Evening all,

I’m having a little trouble with

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

Here is my code:

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;
  let user = '';
  for (user in obj); {
      if(obj[user].online == true){  
      console.log(user);
      counter ++;
      console.log(user); 
      }
    }
    return counter;
  
  // change code above this line
}

console.log(countOnline(users));

At the moment, I am only logging 1 for the countOnline function. I wanted to check my input with further logs on the user, but when I checked my console it returns “Ryan” and “Carl”, which isn’t even in the object.

My questions are:

  1. Why am I skipping over Jeff?
  2. Where did Carl come from?

20

Thanks in advance.


#2

For some reason you have a semi-colon after the for loop closing ). This prevents the loop from iterating passed the first property.

As far as the Carl property showing up, that is something involving the tests behind the scenes. I believe there is a bug with the tests on this challenge which has not been fixed yet. Remove the semi-colon an and your solution will be correct.


#3

Very well spotted. Thank you very much, Randell.