Basic Data Structures - Check if an Object has a Property

Tell us what’s happening:
Describe your issue in detail here.

Hi all,

The challenge is asking me to finish writing the function so that it returns true if the object passed to it contains all four names, Alan , Jeff , Sarah and Ryan and returns false otherwise.

My code, while not the most efficient, does this. However, it won’t pass because I’m not meeting the criteria of

The users object should not be accessed directly

I don’t understand what this means and how I’m accessing it directly. I’m not writing the name of the object directly into the function, it’s being passed in as a parameter or argument in the console.log.

Thanks

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(userObj) {
  // Only change code below this line
  if ("Alan" in userObj == false) {
    return false;
  } else if ("Jeff" in userObj == false) {
    return false 
  } else if ("Sarah" in userObj == false) {
    return false 
  } else if ("Ryan" in userObj == false) {
    return false  
  } else {
    return true
  }
  // Only change code above this line
}

console.log(isEveryoneHere(users));

Your browser information:

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

Challenge: Basic Data Structures - Check if an Object has a Property

Link to the challenge:

I believe this exercise is teaching you about the hasOwnProperty method.

Therefore they are probably expecting you to use it.

The code you posted above passes the challenge when I submit it.

Hey :slight_smile:
it teaches both
users.hasOwnProperty('Alan'); 'Alan' in users;

So I used the second one.

1 Like

Ah! I’ve refreshed and now it passes. Not sure why that is… thanks for replying!

My guess is that you originally had an extra console.log that referenced users and the test checks to make sure you only reference users no more than twice.

The default starting code references it twice. The possible purpose of the test is to make sure you are not changing the users object in the code.

That makes sense Randell. Thanks for taking the time to look into this :slight_smile: