Check if an Object has a Property

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

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.

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
3 Likes

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

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

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

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])
  }
}
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
}
2 Likes

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;

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

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

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

2 Likes

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

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

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

let list = ['Alan', 'Jeff', 'Sarah', 'Ryan']; // create array with names

function isEveryoneHere(obj) {

  let arr = []; // declarate empty array

  for (var i = 0; i < list.length; i++)

    {

  arr.push(obj.hasOwnProperty(list[i])); // push all booleans in the arr

    }

   let sum =  arr.reduce((a, b) => a + b, 0); // find sum of arr: true (1), false (0)
   
   if (sum !== list.length) {  //compare arr sum and list length

     return false

  } else {

     return true

  }
const isEveryoneHere = (obj) => 'Alan' in obj && 'Jeff' in obj && 'Sarah' in obj && 'Ryan' in obj;
console.log(isEveryoneHere(users));

or

const isEveryoneHere = (obj) =>  obj.hasOwnProperty('Alan') && obj.hasOwnProperty('Jeff')  &&  obj.hasOwnProperty('Sarah') &&  obj.hasOwnProperty('Ryan')

This should do the trick:

function isEveryoneHere(obj) {
  // change code below this line
  let check = 0;
  for(let user in obj){
    if(user in obj){
      check++;
    }
  }
  return (check == 4) ? true : false;
  // change code above this line
}

I don’t se how, you are just checking if the object has his own properties - which will return true or false in the way this challenge expect, but it is not what the challenge actually asks from you

 if ('Alan' && 'Jeff' && 'Sarah' && 'Ryan' in users) {
    return true;
  } else {
    return false;
  }
return 'Alan' && 'Jeff' && 'Sarah' && 'Ryan' in users;
return users.hasOwnProperty('Alan','Jeff','Sarah','Ryan');
2 Likes

As much as this pass tests (and please blurry full.working solutions!), these are wrong
Both the in operator and the hasOwnProperty method accept one argument only

Challenge is being changed so that this is considered, with next curriculum update tests will make sure this will not work