Profile Lookup and the "&&" logical operator

Profile Lookup and the "&&" logical operator
0

#1

hi i already solve this exercise but my question is why i can’t use a logical operator (&&)?, when the statement says " The function should check if name is an actual contact’s firstName and the given property ( prop ) is a property of that contact.". I tried to code it like this:

for (var i = 0; i < contacts.length; i++){

if (contacts[i].firstName === name && contacts[i].hasOwnProperty(prop)) {

return contacts[i][prop];

i solved using nested if’s but why it doesn’t pass with the && logical operator?


#2

We would need to see how you are using the code segment you show above with the rest of the code, to be able to tell you why.


#3

Hi thanks for your response this is the entire code.

//Setup
var contacts = [
{
“firstName”: “Akira”,
“lastName”: “Laine”,
“number”: “0543236543”,
“likes”: [“Pizza”, “Coding”, “Brownie Points”]
},
{
“firstName”: “Harry”,
“lastName”: “Potter”,
“number”: “0994372684”,
“likes”: [“Hogwarts”, “Magic”, “Hagrid”]
},
{
“firstName”: “Sherlock”,
“lastName”: “Holmes”,
“number”: “0487345643”,
“likes”: [“Intriguing Cases”, “Violin”]
},
{
“firstName”: “Kristian”,
“lastName”: “Vos”,
“number”: “unknown”,
“likes”: [“JavaScript”, “Gaming”, “Foxes”]
}
];

function lookUpProfile(name, prop){
// Only change code below this line
for(var i = 0; i < contacts.length; i++){
if (contacts[i].firstName === name && contacts[i].hasOwnProperty(prop)){
return contacts [i][prop];
} else {
return “No such property”
}
}
return “No such contact”;
// Only change code above this line
}

// Change these values to test your function
lookUpProfile(“Akira”, “likes”);