Tell us what’s happening:
For every input it shows “No such property”
please help
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
for (var i = 0; i < contacts.length; i++){
if (contacts[i].firstName === name){
if (contacts[i].hasOwnProperty(prop)){
return contacts[i][prop]
} else{
return "No such property";
}
}
return "No such contact"
}
// Only change code above this line
}
lookUpProfile("Akira", "likes");
Your browser information:
User Agent is: Mozilla/5.0 (X11; CrOS x86_64 12739.87.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.128 Safari/537.36
.
Challenge: Profile Lookup
Link to the challenge:
https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/basic-javascript/profile-lookup
You return from the for loop if the firstName
property of the first object in the contacts array does not match name
, ie this will only ever work for the value Akira
. You need to wait until after the for loop to return with "No such contact"
.
1 Like
can you pls tell me the solution
No I can’t, but you can look up the solution on the hints page if you’re ready to give up.
All you need to change is the placement of this line:
return "No such contact"
Right now it’s inside the for loop (before the closing }
), it needs to be outside the for loop (after the closing }
)
2 Likes
cheers i tried it and it works now
Hi, here is my solution to this challenge!
// Only change code below this line
for(var i=0;i<contacts.length;i++)
{
if(contacts[i].firstName===name)
{
if(contacts[i].hasOwnProperty(prop)===true)
{ return contacts[i][prop]; }
else { return “No such property”;}
}
}
for(var i=0;i<contacts.length;i++) {
if(contacts[i].firstName!==name)
{return “No such contact”;}
}
// Only change code above this line
It takes me up to four hours to solve this, I shake the world! I shake the world! I shake the world!!!
function lookUpProfile(name, prop){
// Only change code below this line
for(var a = 0; a < contacts.length; a++){
if(contacts[a].firstName === name){
if(contacts[a].hasOwnProperty(prop)){
return contacts[a][prop];
}
return “No such property”;
}
}
return “No such contact”;
// Only change code above this line
}
var x = lookUpProfile(“Kristian”, “lastName”);
console.log(x);