Tell us what’s happening:
My test is not passing for the No such property, I don’t understand why…My thought was its gonna look for an if condition inside the for loop first and when that condition is not met, the block will go to the second if block outside for loop and finally to the last if statement. Please help me understand what is wrong with the code… I do already know how to do the solution, but I want to understand what’s going on my original code so that I get a clear understanding of how the block of code works. Also, I tried to solve the solution by adding three different for blocks to surround each if statements but did not work as I was expecting. Thank you for your help in advance
Your code so far
function lookUpProfile(name, prop){
// Only change code below this line
for (var i = 0; i < contacts.length; i++){
if (name === contacts[i]["firstName"] && contacts[i].hasOwnProperty(prop)){
return contacts[i][prop];
}
}
if (name !==contacts["firstName"]){
return "No such contact";
}
if (name === contacts["firstName"] && contacts.hasOwnProperty(prop) == false){
return "No such property";
}
// Only change code above this line
}
// Later I tried this code as well
function lookUpProfile(name, prop){
// Only change code below this line
for (var i = 0; i < contacts.length; i++){
if (name === contacts[i]["firstName"] && contacts[i].hasOwnProperty(prop)){
return contacts[i][prop];
}
}
for (var j = 0; j < contacts.length; j++){
if (name !==contacts[j]["firstName"]){
return "No such contact";
} }
for (var k = 0; k < contacts.length; k++){
if (name === contacts[k]["firstName"] && contacts[k].hasOwnProperty(prop) == false){
return "No such property";
}
}
// Only change code above this line
}
```js
//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 (name === contacts[i]["firstName"] && contacts[i].hasOwnProperty(prop)){
return contacts[i][prop];
}
}
if (name !==contacts["firstName"]){
return "No such contact";
}
if (name === contacts["firstName"] && contacts.hasOwnProperty(prop) == false){
return "No such property";
}
// 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/71.0.3578.98 Safari/537.36
.
Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-javascript/profile-lookup