Basic Data structures: Check if an object has a property Apex

Tell us what’s happening:
I am so close to passing ( 5 out of 6 criteria) . I don’t understand why I am failing.

Your code so far

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

function isEveryoneHere(obj) {
  // Only change code below this line
if (users.hasOwnProperty('Alan'+'Jeff'+'Sarah'+'Ryan'== true)) {
return true
} else {
  return false
}

  // Only change code above this line
}

console.log(isEveryoneHere(users)); 
if (users.hasOwnProperty('Alan'+'Jeff'+'Sarah'+'Ryan'== true)) {

So, you are concatenating those strings to be “AlanJeffSarahRyan” and asking if that is equal to true. Since it is not, you are asking:

if (users.hasOwnProperty(false) {

There is not property “false” so it will always evaluate to false.

If you provided a link to the original challenge, we could give clearer advice, but a few things.

First, you can’t check multiple property names like that.

Secondly, the “== true” would have to be outside the method parameter list. (Actually, the “== true” is unnecessary because of how if will evaluate as truthy or falsy whatever it is given - but that’s OK.)

kevinSmith, Thank you for your detailed response. It is most helpful.

I have modified my code accordingly. Subsequently, I have replaced the “+” with “,” ( commas).

Now my code reads as follows:

if (users.hasOwnProperty(‘Alan’,‘Jeff’,‘Sarah’,‘Ryan’== true))

Now, I my code only passes 3 of 6 criteria. Can you provide any additional insight?

To the best of my knowledge, hasOwnProperty only accepts one parameter, so, you are sending 4 params and only the first one is getting read (“Alan”) and the others are getting ignored. I think you are misunderstanding this method - here are the docs.

I think you’re going to have to call that method 4 times. I can’t think off the top of my head a way to do it with just one call.

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