Tell us what’s happening:
I have just started getting into coding and really enjoying it so far, however I have ran into my first problem and I would like some advice on how I could diagnose and solve problems more efficiently as I don’t do the best under pressure.
This is what I typed for my solution. I only got 2/6 tasks correct and both are about the returning “No such contact”. To be perfectly honest I got quite confused on how I should do the first if statement. I have done a for loop and inside the first if statement, I’m trying to check whether the object I’m looping through has “Akira” as its “firstName”.
Then in the second if statement, I checked if the object in the array has both the ‘name’ and ‘prop’ values set by the function. At that point I was going by assumptions that it would stay in the same object where the first if statement found “Akira” in. I’m a beginner and I don’t fully understand how everything works so feel free to point out everything that is wrong or right.
What do you think I can do to solve this problem? I don’t want full answers but clues or tips that will help lead me to a solution. If you could give me some advice or resources I can look up on that will improve problem solving then that would be amazing of you.
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]["firstName"] == name && contacts[i].hasOwnProperty(prop)) {
return contacts[prop];
} else {
return "No such Property";
}
} else {
return "No such contact";
}
}
// 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 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.142 Safari/537.36
.
Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-javascript/profile-lookup