Tell us what’s happening:
**Can anyone tell me whats going on here in code **
Please explain what is in the for loop and rhe if sttement
Thank you!!!
Your code so far
freeCodeCamp Challenge Guide: Iterate Through the Keys of an Object with a for…in Statement
Guide
camperbot
1
Oct '19
Iterate Through the Keys of an Object with a for…in Statement 192
Problem Explanation
Note: dot-notation will cause errors in this challenge.
[square-bracket] notation must be used to call a variable property name.
The following code will not work.
Hints
Hint 1:
for (let user in obj) {
if (obj.user.online === true) {
//code
}
}
Hint 2
Example 2 demonstrates how using [square-bracket] notation the code will be executed.
for (let user in obj) {
if (obj[user].online === true) {
//code
}
}
Solutions
Solution 1 (Click to Show/Hide)
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 result = 0;
for (let user in obj) {
if (obj[user].online === true) {
result++;
}
}
return result;
// change code above this line
}
console.log(countOnline(users));
Your browser information:
User Agent is: Mozilla/5.0 (Linux; Android 8.1.0; CPH1909) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.132 Mobile Safari/537.36.
Challenge: Iterate Through the Keys of an Object with a for…in Statement
Link to the challenge:
https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/basic-data-structures/iterate-through-the-keys-of-an-object-with-a-for...in-statement
