Tell us what’s happening:
Hi All,
Can someone explain why my code is not working? It fails on the first 3 tests but I am unsure why but if I move the (return No such contact) outside of the for loop it works? What am I missing or not understanding - I know I can just pass this as I could change the code to pass but I need to understand the code for my sanity?
“Kristian”, “lastName” should return “Vos”
“Sherlock”, “likes” should return [“Intriguing Cases”, “Violin”]
“Harry”,“likes” should return an array
“Bob”, “number” should return “No such contact”
“Bob”, “potato” should return “No such contact”#“Akira”, “address” should return “No such property”
Thanks,
Wayne
Your code so far
//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
//check if contact exists
for(var i = 0; i < contacts.length; i++){
// Check if the name matches
if(contacts[i].firstName === name ){
//check if the given property exist.
if(contacts[i].hasOwnProperty(prop)){
return contacts[i][prop];
} else {
return "No such property";
}
} else{
return 'No such contact';
}
}
// Only change code above this line
}
// Change these values to test your function
lookUpProfile("Akira", "likes");
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.103 Safari/537.36
.