Check if an Object has a Property(tell me)

Check if an Object has a Property(tell me)
0

#1

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


#2

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


#3

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


#4

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.


#5

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);
}

#6

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


#7

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 .


#8

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
}

#9

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
}```

#10

Wrong…
hasOwnProperty only takes one argument


#11

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
}

#12

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.


#13

You can typically use whatever you want unless the instructions specifically ask you to use a certain method or syntax.

Also, please do not post full working solutions to the challenges. Instead, try to offer suggestions and hints to the original poster as to why their solution does not work. We don’t want to clutter the forum with solutions.

Thank you.


#14

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


#15

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