Exercice: Check if an Object has a Property

Hello,
Following this lesson:
https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/basic-data-structures/check-if-an-object-has-a-property

I try to find the second solution.
With this code bellow, the console return: “TypeError: userObj.indexOf is not a function”.

let tableNames = ['Alan','Jeff','Sarah','Ryan'];
 for (let i = 0; i < tableNames.length; i++) {
   if (userObj.indexOf(tableNames[i]) < 0) {
     return false;
      } 
  return true;
 }

First ,I don’t know if it’s possible to add a “if” condition in a for loop ?
Second, I would like get out of the “if” condition at the first time that indexOf = -1. Is it the case in my code ?
Maye be it’s not possible with this kind of itération (let i = 0; i < tableNames.length; i++), but if it’s possible I would like to know how. Could you please tell me what’s wrong ?

It’s userObj an array (which supports the indexOf function) or an object (which does not)?

let users = {
  Alan: {
    age: 27,
    online: true
  },
  Jeff: {
    age: 32,
    online: true
  },
  Sarah: {
    age: 48,
    online: true
  },
  Ryan: {
    age: 19,
    online: true
  }
};

function isEveryoneHere(userObj) {
  // Only change code below this line

  let tableNames = ['Alan','Jeff','Sarah','Ryan'];
 for (let i = 0; i < tableNames.length; i++) {
   if (userObj.indexOf(tableNames[i]) < 0) {
     return false;
      } 
  return true;
  
  // Only change code above this line
}

console.log(isEveryoneHere(users));

For exemple, if i=0, tableNames[i] = ‘Alan’.
Then, userObj.indexOf( ‘Alan’) = users.indexOf(‘Alan’).

If users.indexOf(‘Alan’) < 0, it’s finished. What is wrong ?

Let me ask again. What kind of thing is userObj? I already know, i know the format…But i need to see if you know.

Sorry, I didn’t understand, I try to speak english/American but …

I replaced “indexOf” by “hasOwnProperty”.

function isEveryoneHere(userObj) {
  // Only change code below this line
  let tableNames = ['Alan','Jeff','Sarah','Ryan'];
 for (let i = 0; i < tableNames.length; i++) {
   if (userObj.hasOwnProperty(tableNames[i]) < 0) {
     return false;
      } 
  return true;
 }

Console say :

hasOwnProperty returns true or false, not a number. So you can simply

if(usersObj.hasOwnProperty(tablenames[i]) ){
  // it is true!
} else {
  // it is false!
}

I tried this but it’s doesn’t work.

let tableNames = ['bob','Jeff','Sarah','Ryan'];
 for (let i = 0; i < tableNames.length; i++) {
   if (userObj.hasOwnProperty(tableNames[i]) < 0) {
     return false;
      } else {
        return true
      }
 }
 return true

Another question:
If userObj.hasOwnProperty(tableNames[i]) = -1, is that the program exits the FOR loop ?

userObj.hasOwnProperty(tablenames[i]) will be true if the name is in, or false if it is not. It will never be >0.

Yes of course I begin to be tired. :upside_down_face:

I tried with this code and I changed the index 1 of tableNames.

function isEveryoneHere(userObj) {
  // Only change code below this line
  let tableNames = ['Alan','bob','Sarah','Ryan'];
 for (let i = 0; i < tableNames.length; i++) {
   if (!userObj.hasOwnProperty(tableNames[i])) {
     return false;
      } else {
        return true
      }
 }

  // Only change code above this line

The console return “true” :rofl:

You need to check for everyone. Can you say that all users are present from inside of this loop?

1 Like

If userObj.hasOwnProperty(tableNames[i]) = false, is that the program exits the FOR loop ?

1 Like

Any time you encounter a return statement, your function immediately stops.

This statement is true. If hasOwnProperty returns false, then yes - immediately break out of the function and return false.

But when should your function return true?

1 Like

If hasOwnProperty returns false, then yes - immediately break out of the function and return false.

When you say “function” you talk about “isEveryoneHere” not just the FOR loop.
Is it correct ?

1 Like

Yes, this is the function that the return statement breaks you out of.

1 Like

this works well by now :rofl:

function isEveryoneHere(userObj) {
  // Only change code below this line
  let tableNames = ['Alan','Jeff','fds','Ryan'];
 for (let i = 0; i < tableNames.length; i++) {
   if (!userObj.hasOwnProperty(tableNames[i])) {
     return false;
      } 
    }
return true

I changed the index 2, console return “false” and when replaced by Sarah, the console return “true”.

It’s cool !
Thank a lot :wink:

1 Like

I wasn’t so far :grinning:
thanks again

1 Like

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