Check if an Object has a Property: What am I doing wrong?

Tell us what’s happening:

I created names to iterate in it in order to verify if each name is in the Obj and a ternary conditional to prompt a boolean value, but I don’t know how to check all the hold names at the same time.

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
  let names = ["Alan", "Jeff", "Sarah", "Ryan"];
  for (let i=0; i<names.length; i++) {
      return  obj.hasOwnProperty == names[i] ?  true  : false;
  }
}

console.log(isEveryoneHere(users));
//I also tried this solution

for (let i=0; i<names.length; i++) {
      return  obj.hasOwnProperty == "Alan", "Jeff", "Sarah", "Ryan" ?  true  : false;

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.142 Safari/537.36 OPR/62.0.3331.119.

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

check how to use hasOwnProperty()

note that if you have a return statement inside a loop, the loop will never go past the first iteration!
as soon a return statement is executed the function return a value and stops

1 Like

I got it ieah.
I was misleading the hasOwnProperty syntax.

function isEveryoneHere(obj) {
    
  return  obj.hasOwnProperty("Alan") ?  true
        : obj.hasOwnProperty("Jeff") ?  true
        : obj.hasOwnProperty("Sarah") ?  true
        : obj.hasOwnProperty("Ryan") ?  true
        : false
}

console.log(isEveryoneHere(users));

now there is something that doesn’t work with the ternary operator!

remember that the ternary operator works like (evaluate this) : (if true do this) ? (else do this), so if “Alan” is there, you are returning true and never checking the others!

1 Like

I think you’re right. I did this:

function isEveryoneHere(obj) {
  // change code below this line
  return obj.hasOwnProperty("Alan", "Jeff", "Sarah", "Ryan") ? true : false
  // change code above this line
}

Indeed, this solution passed the challenge, despite it doesn’t work:

function isEveryoneHere(obj) {
  // change code below this line
 return  obj.hasOwnProperty("Alan") ?  true
        : obj.hasOwnProperty("Jeff") ?  true
        : obj.hasOwnProperty("Sarah") ?  true
        : obj.hasOwnProperty("Ryan") ?  true
        : false  // change code above this line
}

console.log(isEveryoneHere(users));

perhaps it might be a bug…

they are still both wrong, hasOwnProerty() works with only one argument, all the others are ignored
but I am sure you are almost there, these these things need practice

Many questions here:

First of all: I tried this:

return (users.hasOwnProperty("Alan", "Jeff", "Sarah", "Ryan")) ? true : false;

But I guess that’s not useful because one normally could or could not know the object to evaluate in advance or, in any case, I think is not useful write a function just for one object (users)

Second: doesn’t it suppose to be a function with an argument (Obj) that takes any object and verify if it has determined properties, keys?

Anyways: this last solution produce true if all the names are there and false ONLY if all of them are not in.

So, maybe I lost something…

The issue is still the same, it is like you are writing users.hasOwnProperty("Alan") because the method takes one argument only, so it ignores the others

try to use it with an object like

let testObject = {
   Alan: {}.
   Sophie: {}
   Mike: {}
}

it will still give true because it is checking only for the presence of the Alan property

I admit, the tests here are not that well planned, but with a bit of effort you can get the right result!

also, you should always use the function parameter when you are in a function! this is because so the function is reusable

isEveryoneHere(users); // should return true
isEveryoneHere(testObject); // should return false

even if you have the correct code if you don’t use the function parameter inside the function than you will never be able to get different outputs for different inputs