I need help with loops

Tell us what’s happening:

Why is the loop not iterating through all the usersuser

Your code so far

function countOnline(usersObj) {
// Only change code below this line
for(let users in usersObj){
let myRegex=/true/ig;
let matched=[];
let regex=users.match(myRegex);
return matched.length+1;
// Only change code above this line
console.log(countOnline({ Alan: { online: false }, Jeff: { online: true }, Sarah: { online: false } }));

Your browser information:

User Agent is: Mozilla/5.0 (Linux; U; Android 7.0; TECNO P701 Build/NRD90M; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/64.0.3282.137 Mobile Safari/537.36 OPR/52.1.2254.54298.

Challenge: Iterate Through the Keys of an Object with a for…in Statement

Link to the challenge:

return is how you exit a function. So the loop steps once, you return, function ends, nothing else happens (it never looks at anything past the first value).

1 Like

So does it mean loops don’t use return?

you can use return anywhere, but the execution of the function will stop there.
It doesn’t matter if it’s in loop or if-else statement…it will stop the execution of the function you use return in

1 Like

no, you can return in a loop just like any other part, just that if you return the loop will stop, if you want to return multiple values, put everything into an array and then after the loop ends return the array

1 Like

Thank you everyone. I really appreciate your help.
I now have an added knowledge of the return syntax