Check if an Object has a Property: compare 2 tables with ===

Hello,
Check if an Object has a Property
Could you give me the reason why this solution doesn’t work ?

function isEveryoneHere(userObj) {
  // Only change code below this line
  let objNames = Object.keys(userObj);
  console.log(objNames)
  let searchNames = ['Alan','Jeff','Sarah','Ryan']
  if (objNames === searchNames) {
    return true;
  }
return false;
  // Only change code above this line
}

Hello,
Look at this approach:

function isEveryoneHere(userObj) {
  // Only change code below this line
  let objNames = Object.keys(userObj);
  console.log(objNames)
  let searchNames = ['Alan','Jeff','Sarah','Ryan']
  console.log(searchNames)
  console.log(typeof objNames)
  console.log(typeof searchNames)
  if (objNames.toString() == searchNames) {
    return true;
  }

return false;
  // Only change code above this line
}

Try putting it in your code. You are comparing two objects. The result will always be false.

The only reason the code I showed you outputs true is that I used loose equality and not strict equality. What this means is that because I transformed the first element to a string the compiler automatically tries to convert the other element to a string. This approach is not recommended as errors can easily slip in. If you change the ‘==’ with ‘===’ you’ll see that it correctly outputs false. :smiley:

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.