Check if an Object has a Property

Check if an Object has a Property
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
    return obj in users;
  // change code above this line
}

console.log(isEveryoneHere(users));

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.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’re not really doing what it’s asking you.You need to create an if loop, that uses the .hasOwnProperty to check if the names are part of the obj that is passed in. (the obj is the users object WITH the names in your code editor.) You’re basic structure is supposed to look like

if(true){
    return true;
}else{
    return false;
}

where the first ‘true’ condition is the bool variable from obj.hasOwnProperty will give you when you check the names.


#3

There’s a couple things wrong with this code.

  1. you cannot return obj in users, because the in operator is used for iteration (looping)
    • the syntax you want is for 'var' in 'obj'
    • the for starts the loop
    • ‘var’ is the name you assign to the current property in the object
    • obj is the object you are iterating over (looping over)
  2. Since the var variable is the name you assign for the property key during iteration, you can’t place the parameter (obj in this case) there. It should be switched with users

#4

can someone tell me what is wrong with my answer because its not passing the challenge:

if (users.hasOwnProperty(“obj”)) {
return true;
}else{
return false;
}

thank you


#5

i had to adjust my code to be like this and it worked:

if ("Alan" in users) { return true; }else{ return false; }

#6

Pleae check my solution.
I have made the array of the uers and for looped it . It works for me

function isEveryoneHere(obj) {
  let members = [ 'Alan', 'Jeff', 'Sarah',  'Ryan'];
  for (let i = 0; i < members.length; i++) {
   return obj.hasOwnProperty(members[i])
  }
}

#7

@rasheledu

You actually just got lucky with your solution, because the test is faulty. You can prove this to yourself, by testing the following code with your function. You will notice the users object only contains one user (‘Alan’) and none of the other names. So, your function should return false and not true. Instead, it still returns true.

let users = {
  Alan: {
    age: 27,
    online: true
  }
};

function isEveryoneHere(obj) {
  let members = [ 'Alan', 'Jeff', 'Sarah',  'Ryan'];
  for (let i = 0; i < members.length; i++) {
    return obj.hasOwnProperty(members[i])
  }
}

isEveryoneHere(users);

Now the question for you is, why do you think your function does not return false?


#9
function isEveryoneHere(obj) {
  // change code below this line
  return 'Alan' in obj && 'Jeff' in obj && 'Sarah' in obj && 'Ryan' in obj;
  // change code above this line
}

#10

hey guy, I changed your obj to users, It still worked. why?
return ‘Alan’ in users && ‘Jeff’ in users && ‘Sarah’ in users && ‘Ryan’ in users;


#11

if(users.hasOwnProperty(‘Alan’)|| users.hasOwnProperty(‘Jeff’) || users.hasOwnProperty(‘Sarah’) || users.hasOwnProperty(‘Ryan’)){
return true;
} else {
return false;


#12

Hi @Abhi9avcool,

Your solution is incorrect as the criteria is that all users must be in the object.
The test suite must be wrong as your solution evaluate true in any cases. No matter if the user in the object or not.

I created a bin with a non exist name Andy


#13

return obj.hasOwnProperty(“Alan”, “Jeff”, “Sarah”, “Ryan”)? true:false


#14

It is quite troublesome when one doesn’t read the question. Took me some time to realize it. =(

The function has to check if the passed argument, users, contains the names: Alan, Jeff, Sarah and Ryan. And only returns true if the four names are in obj.

A loop to check each name, accumulated in an array, to verify it exists in obj is fine

here is my function but saw a very nice and shorter alternative before.

function isEveryoneHere(obj) {
let names = ["Alan", "Jeff", "Sarah", "Ryan"];
  let ok = 0;
  for (let name of names) {
    if (name in obj) {
      ok++;
    } else {
      return false;
    }
  }
  if (ok === 4) return true;
}

#15

I think it is because the function has access to the outer scope.


#16

Hey guys, quick question, the hint solution uses commas (,) instead of the “and” operator in the if statement, but I found out that even if you add more properties afetr the commas that are not in the object, it still evaluates to true. Is my solution below (it passed the test) the best way or am I missing something?

function isEveryoneHere(obj) {
if (obj.hasOwnProperty(‘Alan’ && “Sarah” && “Jeff” && “Ryan”)) {
return true;
}
return false;
}