Challenge URL : https://www.freecodecamp.com/challenges/profile-lookup
//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) {
if (contacts[i].hasOwnProperty(prop)) {
result = contacts[i];
}
else {
result = i;
}
}
}
if (typeof result == "number") {
return "No such property";
}
else if (typeof result == "undefined") {
return "No such contact";
}
else {
return result[prop];
}
// Only change code above this line
}
// Change these values to test your function
lookUpProfile("Bob", "number");
Description:
lookUpProfile("Bob", "number") has returned "No such contact" but can't pass through the test.
For me, it's returning "No such property" and it should be "No such contact". So for now, I'm unable to reproduce and this seems to be a code error, but others please chime in.
This one was tricky to figure out. Your variable _result_ isn't declared in your function so it's scope is much larger than you intended, the solution is to just declare var result; at the beginning of your function so it 'resets'/starts as undefined when the function is called again for the next test case.
In it's current state _result_ is retaining the info as it has a global scope and passing it to the next test case... so when it tests bob and DOESNT set a value for result, it actually keep Harry's object as its value.
Closing as resolved?
Well Done Mate,
it looks me a while to figure it out . Thanks
Most helpful comment
This one was tricky to figure out. Your variable _result_ isn't declared in your function so it's scope is much larger than you intended, the solution is to just declare var result; at the beginning of your function so it 'resets'/starts as undefined when the function is called again for the next test case.
In it's current state _result_ is retaining the info as it has a global scope and passing it to the next test case... so when it tests bob and DOESNT set a value for result, it actually keep Harry's object as its value.