mqz
July 22, 2020, 7:39pm
1
Tell us what’s happening:
I’m not getting the right result and have no idea where/what detail I’m missing
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(usersObj) {
// Only change code below this line
let result = 0;
for(let user in countOnline) {
if (obj[users].online === true) {
result++;
}
} return result;
// Only change code above this line
}
Your browser information:
User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:77.0) Gecko/20100101 Firefox/77.0
.
Challenge: Iterate Through the Keys of an Object with a for…in Statement
Link to the challenge:
Learn to code. Build projects. Earn certifications.Since 2015, 40,000 graduates have gotten jobs at tech companies including Google, Apple, Amazon, and Microsoft.
ILM
July 22, 2020, 7:40pm
2
your function has a parameter with value of an object which you are never using
also you have for user in ...
but then you use the variable users
obj[users]
has a double issue: obj
is undefined, users
is undefined
1 Like
This is why we needed to see your full code. countOnline
is not an object you can iterate through! You need to iterate through your object of users.
mqz
July 23, 2020, 5:40pm
4
Thanx for the input JeremyLT
mqz
July 23, 2020, 5:54pm
5
Thanx for your input JeremyLT
Like JeremyLT said you need to loop over the correct object and not the function.
But just as an aside. Fun fact, a function is an object and can be iterated using for…in. I’m just not sure why you would ever do so.
function countOnline() {
for (let user in countOnline) {
console.log(user); // propOnFunction
console.log(countOnline[user]); // test
}
}
countOnline.propOnFunction = 'test';
countOnline();