The problem is that the first if condition has a problem that I think is trivial but I can't figure it out ath the same time ..
Challenge Profile Lookup has an issue.
User Agent is: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.84 Safari/537.36
.
Please describe how to reproduce this issue, and include links to screenshots if possible.
My 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(firstName, prop){
// Only change code below this line
for ( var i = 0; i < contacts.length; i++) {
if ( contacts[i].firstName === firstName && contacts[i].hasOwnProperty(prop)) {
return contacts[i][prop];
}
else if (firstName !== contacts[i].firstName) {
return "No such contact";
}
else if (contacts[i].hasOwnProperty(prop) === false) {
return "No such property";
}
}
// Only change code above this line
}
// Change these values to test your function
lookUpProfile("Harry", "likes");
I think this challenge is designedly bad. The three incorrect answers from your code were the ones that had property with value(s), weren't they? For example, lookUpProfile("Sherlock", "likes");. Well, when the for-loop runs, it will not iterate from 0 to contacts.length - 1. The value of i will only be 0. Do you see why? After the first run, there will be a return statement no matter what. Therefore, the for-loop (and the function) will end.
No matter how designedly bad the challenge is, there are solutions. Here is mine if you want to take a look at it
function lookUpProfile(firstName, prop) {
// Only change code below this line
for (var i = 0; i < firstName.length; i++) {
if (contacts[i].firstName === firstName) {
if (contacts[i].hasOwnProperty(prop))
return contacts[i][prop];
return "No such property";
}
}
return "No such contact";
// Only change code above this line
}
P.s: If you have coded in complied programming languages, such as C, C++, Java, etc. (I'm not sure if this is JavaScript or the compile that FreeCodeCamp is using), when you use if-else statement, there will/must always be an "else" at the end of this if-else statement (you has else if).
Ok, you have made a good point here, and I tried your code but I found 2 consecutive returns, can you post a picture so I can see how your code indentation? I really appreciate your help :+1:
Mine works just fine.
@AhmedElkashef02 @pitlv2109 looks like the issue is resolved. If you get stuck with a challenge, please first ask questions to the Help Room. Happy coding!
Thank you for posting. I got stuck on this challenge and this helped me.
I still have one question though: Shouldn't the for loop be using contacts.length instead of firstName.length?
I have the same question as @romort
I tried both and both work perfectly. Why is firstName.length correct? Is it just because both lengths are equal to 4?
solution:
function lookUpProfile(name, prop)
{
// Only change code below this line
for (var p in contacts)
{
if (contacts[p].firstName === name)
{
if (!contacts[p].hasOwnProperty(prop))
{
return 'No such property';
}
return contacts[p][prop];
}
}
return "No such contact";
// Only change code above this line
}
Most helpful comment
I think this challenge is designedly bad. The three incorrect answers from your code were the ones that had property with value(s), weren't they? For example, lookUpProfile("Sherlock", "likes");. Well, when the for-loop runs, it will not iterate from 0 to contacts.length - 1. The value of i will only be 0. Do you see why? After the first run, there will be a return statement no matter what. Therefore, the for-loop (and the function) will end.
No matter how designedly bad the challenge is, there are solutions. Here is mine if you want to take a look at it
P.s: If you have coded in complied programming languages, such as C, C++, Java, etc. (I'm not sure if this is JavaScript or the compile that FreeCodeCamp is using), when you use if-else statement, there will/must always be an "else" at the end of this if-else statement (you has else if).