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

Tell us what’s happening:
I just don’t know what happened, it’s so strange!

These code lead to an unknow result which has 3 item more, they are Harry, Sam, and Carl, where are they come from?

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 i = 0;
for (let name in obj) {
 console.log(name)
 if (obj[name].online===true) {
 i++; 
 }
}
return console.log(i);
  // 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/75.0.3770.142 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

Thanks, this video’s solution has little flaw, he used the users directly instead of using the arguments “Obj”, I have pass it, but still confuse about the 3 more item from the console.log output, very strange.

It is coming from the test (it has been rewritten on master so it was a bit harder to find the current old test)

  - text: The <code>users</code> object contains users <code>Jeff</code> and <code>Ryan</code> with <code>online</code> set to <code>true</code> and users <code>Alan</code> and <code>Sarah</code> with <code>online</code> set to <code>false</code>
    testString: assert(users.Alan.online === false && users.Jeff.online === true &&  users.Sarah.online === false &&  users.Ryan.online === true, 'The <code>users</code> object contains users <code>Jeff</code> and <code>Ryan</code> with <code>online</code> set to <code>true</code> and users <code>Alan</code> and <code>Sarah</code> with <code>online</code> set to <code>false</code>');
  - text: The function <code>countOnline</code> returns the number of users with the <code>online</code> property set to <code>true</code>
    testString: 'assert((function() { users.Harry = {online: true}; users.Sam = {online: true}; users.Carl = {online: true}; return countOnline(users) })() === 5, ''The function <code>countOnline</code> returns the number of users with the <code>online</code> property set to <code>true</code>'');'

try to understand when use normal for loop and for in loop and for of loop!
Do you know ?

Cooool,I got it, thanks very much!

I’m not sure what’s your problem, it’s so tricky:thinking:

This challenge is to teach you how you can work with object.
if you look carefully you can see that

let users={{object1},
{object2},{object3}.
}

this is objects warped inside another object. As you know that length of the object is undefined, you can not use for loop here. so you have to use for in loop or for of loop
if it was an array
it should be warped into a squire bracket

users=[{object1},
{object2},{object3}.
}]

now this is an array which is the collection of three object. and the length of the array is 3. so it is irritable . so you can use normal for loop

So clear, I get it, Thanks for your earnest illustration.

there is a different function called by the tests that uses a different object to test

you are not passing tests because of return console.log(...), you can’t return a console.log. well, you can, but you are just returning undefined. You need two different lines, one for logging to the console, the other for returning the value

1 Like