Address me please about function notation

Tell us what’s happening:

Hi,
I have noticed this second solution for the test :
function isEveryoneHere(obj) {
return [“Alan”, “Jeff”, “Sarah”, “Ryan”].every(name =>
obj.hasOwnProperty(name)
);
}

But I can’t read the method (function?) properly.Where we can find in our FreeCodeCamp lesson something about this notation? And how can I understand when to use it instead of a more classic one? I can see it may be an arrow function though…

Plus, do you know why the method do not evaluate an expression like this one and does not work: obj.hasOwnProperty(‘Alan’&&‘Jeff’&&‘Sarah’&&‘Ryan’);
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) {
// Only change code below this line
  return obj.hasOwnProperty("Alan") &&
  obj.hasOwnProperty("Jeff") &&
  obj.hasOwnProperty("Sarah") &&
  obj.hasOwnProperty("Ryan")
// Only change code above this line
}

console.log(isEveryoneHere(users));

/*function isEveryoneHere(obj) {
return ["Alan", "Jeff", "Sarah", "Ryan"].every(name =>
  obj.hasOwnProperty(name)
);
}
*/

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.61 Safari/537.36.

Challenge: Check if an Object has a Property

Link to the challenge:

Hello @lorenzo0572, Here’s an explanation how the .every() function works:

  • [“Alan”, “Jeff”, “Sarah”, “Ryan”].every( - The .every() checks if every item inside the array passes the conditional test, which is a function put inside the parantheses as a parameter.

  • name => - As you know it, the ES6 Arrow Function that takes a parameter of name. In this case, the parameter passed is going to be form the Array provided from the .every(); method.

  • obj.hasOwnProperty(name) - This checks if obj, which is the paramter of the parent function, (hasOwnProperty)has a property of the parameter name from the arrow function, which is the Items inside the array given by the .every(); method.

  • This is going to return 2 things only: True or False. True if obj parameter contains all of the items from the array given. False if it doesn’t contain one of the items from the array given.

Hope this helps. If you need more explanations on specific details, please ask, so I can give it a more detailed explanation :slight_smile: Happy Coding.

hasOwnProperty wants a single argument
the expression 'Alan'&&'Jeff'&&'Sarah'&&'Ryan' evaluates to a single element, which I admit don’t remember how to figure out, but you can see what it evaluates to using console.log('Alan'&&'Jeff'&&'Sarah'&&'Ryan')
the string to which it evaluates is the only one checked by hasOwnProerty
to use hasOwnProperty with all the strings you need to do obj.hasOwnProperty("Alan") && obj.hasOwnProperty("Jeff") ...