2

I've been having some trouble with the Free Code Camp lesson on checking if an object has certain properties.

In this lesson, we're supposed to use hasOwnProperty() to check if the users object contains Alan, Jeff, Sarah, and Ryan. If all of the users are present, then return true, otherwise, if any of those users are missing, then the code needs to return false.

I've been trying for over hour and this is kind of where I've ended up, but I can't quite figure out how to get the code to return false when one of the names is removed. I tend to overthink my code, so I might be thinking about it too hard.

Thanks in advance! And sorry if something like this has been asked before. I wasn't able to find anything.

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
  for (let name in users) {
    if (name === 'Alan' && 'Jeff' && 'Sarah' && 'Ryan') {
      return true;
    } else {
      return false;
    }
  }
  // change code above this line
}

console.log(isEveryoneHere(users));
Get Off My Lawn
  • 34,175
  • 38
  • 176
  • 338
darmeg
  • 69
  • 7
  • 1
    start a counter at 0, add 1 when name is Alan, Jeff, Sarah or Ryan. Then when loop ends check if counter is equal to 4 – juvian Jun 26 '18 at 20:37
  • apart from the incorrect condition in your if statement, your code returns false when i run it in the developer console without the return statements... – Harry Jun 26 '18 at 20:46

7 Answers7

3

You don't need to loop through the properties. You should loop through the array of names you're looking for.

function isEveryoneHere(obj) {
  // change code below this line
  for (let name of required) {
    if (!obj.hasOwnProperty(name)) {
      return false;
    }
  }
  return true;
  // change code above this line
}

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

const required = ['Alan', 'Jeff', 'Sarah', 'Ryan'];

console.log(isEveryoneHere(users));
console.log(isEveryoneHere(users2));
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • This one is really simple and easy to understand! I appreciate the second array to show how the function reacts when one of the parameters isn't present. Thanks! quick question, since the lesson didn't really explain much: why do you need the '!' in `!obj`? – darmeg Jun 26 '18 at 20:53
  • 1
    That's the "not" operator. It returns false when the required name is *not* present. – Barmar Jun 26 '18 at 20:53
1

I would loop through each person, and if the value is good continue if not return false. If we make it to the end of the loop we return true.

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
  for (let name in obj) {
    if(!obj.hasOwnProperty(name) || !obj[name].online){
      return false;
    }
  }
  return true;
  // change code above this line
}

console.log(isEveryoneHere(users));
Get Off My Lawn
  • 34,175
  • 38
  • 176
  • 338
1

Give this a go. There are many ways to do it but this was an elegant solution. I chopped the idea from here.

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
  var arr = ["Alan", "Jeff", "Sarah", "Ryan"];
  var hasAllKeys = arr.every(function(item){
    return obj.hasOwnProperty(item);
  });
  return hasAllKeys;
  // change code above this line
}

isEveryoneHere(users);
Joseph Cho
  • 4,033
  • 4
  • 26
  • 33
  • Thanks! This is really similar to another person's answer which passed my test. I like the simplicity in this answer. Thanks very much for helping out! – darmeg Jun 26 '18 at 20:57
0

In the example, the statement "Alan" in users returns 'true'. Why can't you do something like:

'Alan' in users && 'Sarah' in users && 'Jeff' in users...etc.

??

Will
  • 7
  • 1
  • 4
0

The for in loops through each property name and stores in an array. If the obj being passed does not have the property of the array(name) returns false.

let required = [];
for (let user in users){
  required.push(user);
}
function isEveryoneHere(obj) {
  // change code below this line
  for (let name of required) {
    if (!obj.hasOwnProperty(name)) {
      return false;
    }
  }
  return true;
  // change code above this line
}
Will
  • 1
  • 1
0

this works

    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) {

if(users.hasOwnProperty('Alan', 'Jeff','Sarah','Ryan')){
  return true;
}
  return false;

}

console.log(isEveryoneHere(users));
user163169
  • 135
  • 2
  • 9
0

I wrote this and it works:

function isEveryoneHere(obj) {
  // Only change code below this line
  let filter = ['Alan', 'Jeff', 'Sarah', 'Ryan'];
  let result = true;
  filter.forEach(
    (u) => {
      if(!obj.hasOwnProperty(u)){
        result = false;
      }
    }    
  );     
  return result;
  // Only change code above this line
}
dougparnoff
  • 61
  • 1
  • 7