Hello. I would want to know what is wrong with my code. It seems to work with the first contact, but it doesn´t with the other ones.
Task: The function should check if name is an actual contact’s firstName and the given property ( prop ) is a property of that contact.
If both are true, then return the “value” of that property.
If name does not correspond to any contacts then return "No such contact" .
If prop does not correspond to any valid properties of a contact found to match name then return "No such property" .
This is my script:
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++) {
//for (var j=0; j < contacts[i].length; j++) {
//console.log(contacts[i]);
if (contacts[i].firstName === name) {
// | significa y si no se cumple la condición
return contacts[i][prop];
} else if (contacts[i].firstName !== name) {
return "No such property";
} else {
return "No such contact";
}
}
}
// Only change code above this line
console.log(lookUpProfile("Harry", "lastName"));
