Basic Data Structures: Iterate Through the Keys of an Object with a Statement and (Ternary) Operator

Tell us what’s happening:
I tested the function on vscode and it returns the correct result but I can’t pass the three test cases at the challenge

Your code so far

function countOnline(usersObj) {
    // Only change code below this line
    let counter = 0;
    // for (const key in usersObj) {
    //     if (usersObj.hasOwnProperty(key)) {
    //         const element = usersObj[key].online;
    //         console.log(element);
    //     }
    // }
    for(userObj in usersObj){
        // console.log(usersObj[userObj].online);
        // (usersObj[userObj].online == true)? console.log(++counter) : counter;
        (usersObj[userObj].online == true)? (++counter) : counter;

    return counter;

    // Only change code above this line

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.61 Safari/537.36.

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

Link to the challenge:


A simple typo here. You’re not declaring userObj correctly so it is undefined. The proper syntax is for (let x in y). :slight_smile:

1 Like