Hi, just thought I came back here for practice.
Last time I completed this challenge with the help of the forum. Here I am again, except I think I got the code right except for indentations and curly braces that have always been a problem for me. Can someone please tell me where to close curly braces without messing everything up?
Here’s the complete 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(name, prop){
// Only change code below this line
for(var i = 0; i < contacts.length; i++) {
if(name === contacts[i].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");
@JeremyLT As I understand, the closing of the for loop should be the one before the last, the last being the closing of the function. I had forgotten the semicolons after “No such property” and “No such contact”. I think I arranged that with your help, but still isn’t working. Is it right what I just stated?
Here’s the updated 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(name, prop){
// Only change code below this line
for(var i = 0; i < contacts.length; i++) {
if(name === contacts[i].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");
JeremyLT is Right. You are stoping execution by using the return “No such contact” before incrementing " i ".
I was making the same mistake, thanks to chrome dev tools where you can see step-by-step execution of your code and it really helps to understand what’s happening behind the scenes.