Check if an Object has a Property(tell me)

Tell us what’s happening:

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) {
  // change code below this line
  if(users.hasOwnProperty(obj)=true){
    return true;
  }else{

  return false;

  // change code above this line
}

console.log(isEveryoneHere(users));

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36.

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-data-structures/check-if-an-object-has-a-property

You didn’t answer the “Tell us what’s happening prompt”.

Hi @nchamoli,

If you ask about why your code give you an error, here is the answer:

Firstly, you should understand what hasOwnProperty() and in do in object, both of them wanted to know if an object has a specific property or not.

Secondly, to check one object by these two syntax you should do like this:

users.hasOwnProperty('Alan'); //this by using hasOwnProperty()
'Alan' in users; //this by using in
// both return true

After understanding these two important things, we will going to your code, the problem in your code is because you check for one name in users object, so, you need to check all names by using && operator.

If you pinned in example, you can check the answer from here, good luck and have a happy coding :slight_smile:

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

console.log(isEveryoneHere(users));

Hope this will help you my friend, good luck.

Regards,
Ali Mosaad

2 Likes

You have to check if Alan, Jeff, Sarah, and Ryan exit on the object users. So you have to find out how to loop through all the users and check.

I wrote a solution for people who want to know how to build this with in and every():

function isEveryoneHere(obj) {
  // the following names should be searched for
  const names = ['Alan', 'Jeff', 'Sarah', 'Ryan'];

  // return true if every inner check returns true
  return names.every((name) => {
    // return true if the current element is in "obj" (= "users" array)
    return name in obj;
  });

  // shorter syntax: return names.every((name) => name in obj);
}
5 Likes

@miku86 Thank you, I was looking for a shorter solution (as “if( && && &&…” seemed unnecessary too long) but didn’t know about “every” and “in”.

obj is users it is a parameter and users is the argument of it ,so you sould do this :
if(obj.hasOwnProperty(“Alan”)==true ) // also here you should not use the assignment operator (=) , rahter of that you have to use strict opertor (==) ,and finally you should add the same condition for the second property by the and operator (&&) to collect all the conditions as one condition so that they should all be true to pass the challenge .

This is the way i passed it

function isEveryoneHere(obj) {
  // change code below this line
    if (obj.hasOwnProperty('Alan' && 'Jeff' && 'Sarah' && 'Ryan')) {
      return true;
    } else {
      return false;
    }
  // change code above this line
}
1 Like

I think there is no need for (&&) , I passed it like this :

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

  // change code above this line
}```
2 Likes

Wrong…
hasOwnProperty only takes one argument

This is my solution. I like better the “in” operator approach (bit more pythonic :slight_smile:)

function isEveryoneHere(obj) {
  // change code below this line
  let names = ['Alan', 'Jeff', 'Sarah', 'Ryan'];
  //let check = names.map(x => users.hasOwnProperty(x));
  let check = names.map(x => x in users);
  return (check.includes(false) ? false : true);
  // change code above this line
}

A simple solution using a basic for loop, using the .hasOwnProperty() method.


function isEveryoneHere(obj) {
  // change code below this line
  let names = ['Alan', 'Jeff', 'Sarah', 'Ryan'];
  for (let i = 0; i < names.length; i++) {
    if (obj.hasOwnProperty(names[i])) {
      return true;
    } else {
      return false;
    }
  }
  // change code above this line
}

console.log(isEveryoneHere(users));

I am a little confused, as, if i’m not mistaken, the usage of in, every(), map() has not been introduced to us at this point. I was wondering if we are we encouraged to utilize subject matter which has not been covered in the curriculum yet.

Thank you very much for clarifying. And I understand, I will take care in future to post accordingly. Thank you again!

of course it can. hasOwnProperty can check as many properties that exist in the object.

Short version of the solution (include original object and console.log):

function isEveryoneHere(obj) {
  return 'Alan' && 'Jeff' && 'Ryan' && 'Sarah' in users ? true : false;
}

Why not?
freeCodeCamp accepted it as a valid solution, repl.it also accepts it fine.

I see, very interesting!
Yes, you’re right, when only 'Sarah' is in object is returns true