** Help Needed ** Check if an Object has a Property

Tell us what’s happening
I passed all criteria except for this:
“The users object should not be accessed directly”. Not sure what I did wrong here. Could someone please explain?

Link to challenge:

Code:

let users = {
  Alan: {
    age: 27,
    online: true
  },
  Jeff: {
    age: 32,
    online: true
  },
  Sarah: {
    age: 48,
    online: true
  },
  Ryan: {
    age: 19,
    online: true
  }
};

function isEveryoneHere(userObj) {
  // Only change code below this line
  if ('Alan' in users && 'Jeff' in users && 'Sarah' in users && 'Ryan' in users) {
    return true;
  } else {
    return false;
  }
  // Only change code above this line
}

console.log(isEveryoneHere(users));

Thanks in advance!

You shouldn’t type this in your function.

You must use the argument instead.

2 Likes

Thanks, that worked. But what’s the reason for not using “users”?

How does the function know to look for the objects in “users” if I don’t explicitly write “users”?

The function call says which object you are using. The function definition says what you are doing with the object you choose to use.

1 Like

The point of using a function is reusability - if you access “users” directly, why even go through the trouble of writing all that fluff around? It would be pointless.
However if you want to check a bunch of objects, you can write one function and give each object as argument to get a result.

Also on the note of results: if the statement in the “if (stuff)” evaluates as true, it will go into the if-branch, if not, into the else branch.
Meaning: if (statement) {return true} else {return false} is the same is return statement

2 Likes

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.