Tell us what’s happening:
I’ve solving this problem
My function code doesn’t return value, and return only “No such contact”
when I try this function on console.
- lookUpProfile(“Kristian”, “lastName”);
return -> “No such contact” (it should be “Vos”)
number 2,3 are same problem, it doesn’t return value even if condition is satisfied.
2.lookUpProfile(“Sherlock”, “likes”);
“No such contact”
3.lookUpProfile(“Harry”,“likes”);
“No such contact”
So I tried this “If condition” on console
contacts[3][“firstName”] == “Kristian” && contacts[3].hasOwnProperty(“lastName”)
But It doesn’t have error It return
true
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){
for(var i=0; i<contacts.length; i++){
var value;
if(contacts[i]["firstName"] == name && contacts[i].hasOwnProperty(prop)){
value = contacts[i][prop];
}
else if(contacts[i]["firstName"] == name && !contacts[i].hasOwnProperty(prop)) {
value = 'No such contact';
}
else if(contacts[i]["firstName"] != name) {
value = 'No such contact';
}
return value;
}
}
// 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/69.0.3497.100 Safari/537.36
.
Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-javascript/profile-lookup