--description--
Sometimes you need to iterate through all the keys within an object. You can use a for...in loop to do this. The for...in loop looks like:
const refrigerator = {
'milk': 1,
'eggs': 12,
};
for (const food in refrigerator) {
console.log(food, refrigerator[food]);
}
This code logs milk 1
and eggs 12
, with each key-value pair on its own line.
We defined the variable food
in the loop head and this variable was set to each of the object's keys on each iteration, resulting in each food's name being printed to the console.
NOTE: Objects do not maintain an ordering to stored keys like arrays do; thus a key's position on an object, or the relative order in which it appears, is irrelevant when referencing or accessing that key.
--instructions--
We've defined a function countOnline
which accepts one argument, allUsers
. Use a for...in statement inside this function to loop through the allUsers
object and return the number of users whose online
property is set to true
. An example of an object which could be passed to countOnline
is shown below. Each user will have an online
property set to either true
or false
.
{
Alan: {
online: false
},
Jeff: {
online: true
},
Sarah: {
online: false
}
}
--hints--
The function countOnline
should use a for in
statement to iterate through the object keys of the object passed to it.
assert(
__helpers.removeJSComments(code).match(
/for\s*\(\s*(var|let|const)\s+[a-zA-Z_$]\w*\s+in\s+[a-zA-Z_$]\w*\s*\)/
)
);
The function countOnline
should return 1
when the object { Alan: { online: false }, Jeff: { online: true }, Sarah: { online: false } }
is passed to it
assert(countOnline(usersObj1) === 1);
The function countOnline
should return 2
when the object { Alan: { online: true }, Jeff: { online: false }, Sarah: { online: true } }
is passed to it
assert(countOnline(usersObj2) === 2);
The function countOnline
should return 0
when the object { Alan: { online: false }, Jeff: { online: false }, Sarah: { online: false } }
is passed to it
assert(countOnline(usersObj3) === 0);
--seed--
--after-user-code--
const usersObj1 = {
Alan: {
online: false
},
Jeff: {
online: true
},
Sarah: {
online: false
}
}
const usersObj2 = {
Alan: {
online: true
},
Jeff: {
online: false
},
Sarah: {
online: true
}
}
const usersObj3 = {
Alan: {
online: false
},
Jeff: {
online: false
},
Sarah: {
online: false
}
}
--seed-contents--
const users = {
Alan: {
online: false
},
Jeff: {
online: true
},
Sarah: {
online: false
}
}
function countOnline(allUsers) {
// Only change code below this line
// Only change code above this line
}
console.log(countOnline(users));
--solutions--
function countOnline(allUsers) {
let numOnline = 0;
for(const user in allUsers){
if(allUsers[user].online) {
numOnline++;
}
}
return numOnline;
}