Tell us what’s happening:
Describe your issue in detail here.
I’m not that sure about this, cant explain through my head…
the code run
/*return collection.filter(x => {
setting second loop to run each…
for(let i = 0; i < srcKey.length; i++){
here’s the issue, if is not false ||
if(!x.hasOwnProperty(srcKey[i]) || ----->if is not false … or
x[srcKey[i]] !== source[srcKey[i]] -------> if is not false
){
return false. ------> both true return to false?
}
}
return true. ----- > or stop and return true?
still cant figure it out…
**Your code so far**
function whatIsInAName(collection, source) {
let srcKey = Object.keys(source);
/*console.log(srcKey[0])
console.log(source[srcKey[0]])
console.log(collection[0]["last"])
console.log(collection[0].hasOwnProperty('first'))
console.log(collection[0][srcKey[0]])
// Only change code below this line
*/
/*return collection.filter(x => {
for(let i = 0; i < srcKey.length; i++){
if(!x.hasOwnProperty(srcKey[i]) ||
x[srcKey[i]] !== source[srcKey[i]]
){
return false
}
}
return true
})*/
// Only change code above this line
return collection.filter(x => {
for(let i = 0; i < srcKey.length; i++){
if(true){
console.log("prop" + !x.hasOwnProperty(srcKey[i]))
console.log(x[srcKey[i]] !== source [srcKey[i]])
}
} return true})
}
console.log(
whatIsInAName([{ first: "Romeo", last: "Montague" }, { first: "Mercutio", last: null }, { first: "Tybalt", last: "Capulet" }], { last: "Capulet" }));
**Your browser information:**
User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.45 Safari/537.36
!x.hasOwnProperty(srcKey[i]) says “take the Boolean returned by x.hasOwnProperty(srcKey[i]) and flip it.” So, if x has that property, the x.hasOwnProperty() would return true, but the ! would flip it to false.
That reads in plain English as “if xdoes not contain the given property…”
Now if that condition is true, that x doesn’t have that property, then the entire if statement is true. In the case of ||, the first “truthy” value renders the entire expression true.
If that condition is not met (if x does have that property), then we start evaluating after the ||:
x[srcKey[i] ] !== source[srcKey[i] ] reads as “the given property in xdoes not match the same property in source.” In the event they are not equal, this half of the if resolves to true… Which renders the entire expression as true.
In the case of ||, the first “true” value causes the entire expression to be true. In the case of && the first “false” value causes the entire expression to be false.
So
// after the or doesn't evaluate.
true || false === true
true || true === true
// the evaluation continues until the
// first true, but the entire expression is
// true!
false || false || false || true === true
false || true === true
// if none are true, then the entire thing
// is false
false || false || false === false
// in the opposite way, &&
// evaluation stops at the first false
// the second is never evaluated
false && true ===false
false && false ===false
// evaluation continues to the first false
true && false === false
true && true && true && false === false
So, in this case, if the first thing is true or the second thing is true, return false. Otherwise, return true. This only happens if both ! (NOT) conditions are true: