Better way to solve this challenge

Better way to solve this challenge
0

#1

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

I solved the challenge hard coding it as followed:

function isEveryoneHere(obj) {
  // change code below this line
   if ('Alan' in obj && 'Jeff' in obj && 'Sarah' in obj && 'Ryan' in obj) {
    return true;
  } else {
    return false;
  }
  // change code above this line
}

It’s easy to see why this solution is not the best. A loop would be ideal for a situation like this, but what kind of loop do I need to iterate an object as shown in the challenge?


#2

In general if you have code that looks like:

if (condition) {
  return true;
} else {
  return false;
}

you can do it this way:

return condition;

If you want to use a loop, you may want to start with putting the names in an array first, then use any looping statement you prefer to use. This has the obvious advantage of being able to easily add/remove names that you want to test.

var names = ['Alan', 'Jeff', 'Sarah', 'Ryan'];
for (var i = 0; i < names.length; i++) {
  // ...
}

Or you could use built-in array utilities, like .reduce(). There’s actually a built-in array function that makes this very trivial


#3

Hey Gilbert,
Did you read the instructions given in this challenge?
The challenge teaches about a method hasOwnProperty()
Read the instructions carefully and you would be able to do it.
Hope this helps.


#4

If I had a 100 names I’d still need a loop to add those names to an array, I managed to find the right loop to iterate an object. This was my solution after struggling a bit:

function isEveryoneHere(obj) {
  // change code below this line
  const names = [];

  for(let key in obj) {
    names.push(key)
  }
 return names.every(user => !user) ? false: true;
  // change code above this line
}

However I have a doubt, if I say:

return names.every(user => user) ? true: false;

It doesn’t work. According to MDN, the every() method tests whether all elements in the array pass the test, which in this case is that every element occurs in the array, and if it’s true, then return true. But this logic won’t work, instead I had to use the not operator and change the logic. Why?


#5

Your code doesn’t seem quite right. Suppose you removed Ryan from the object. It still returns true.
See https://repl.it/repls/PointlessShabbyPublishing

The reason being, every name is a truthy value. So inside every(), user => !user will always be false. When every hits a false from its callback function, it immediately returns false. Compound that with the tertiary expression you have, and you’ll always get true. It doesn’t matter if all of the names required are in the names array.

Your function needs to have the names coded in it.

You can’t just inverse the value by negating the return value of the callback, then reversing the last values of the tertiary. You can properly inverse by replacing every() with some()


#7

this is my code …
Screenshot_20180824_163956