Tell us what’s happening:
I don’t know why the console says there is an error. I’ve tried to debug my code for hours but found no mistake so far. can u help me to find out my mistake
**Your code so far**
// Setup
var contacts = [
{
"firstName": "Akira",
"lastName": "Laine",
"number": "0543236543",
"likes": ["Pizza", "Coding", "Brownie Points"]
},
{
"firstNam e": "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].prop==undefined){return "No such property" }
else if(contacts[i].prop!=undefined){return contacts[i].prop;}
}
else {"No such contact"}}
// Only change code above this line
}
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/90.0.4430.212 Safari/537.36.
I’ve formatted your code, maybe you can see there are issues with the syntax
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].prop == undefined) {
return "No such property"
} else if (contacts[i].prop != undefined) {
return contacts[i].prop;
}
};
else {
"No such contact"
}
}
// Only change code above this line
}
for example you can’t put an else after a semicolon
I’ve resolved all the syntax error but still, it’s not working plz help
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].prop==undefined){return "No such property" }
else if(contacts[i].prop!=undefined){return contacts[i].prop;}}
else{"No such contact"}``
// Only change code above this line
}}
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].prop==undefined) {
return "No such property"
} else if (contacts[i].prop!=undefined) {
// The condition is the exact opposite of the one above
// That means you don't need an else if and can use an else
return contacts[i].prop;
}
} else {
// What are you doing here?
// There are two problems with this
// One is syntax and one is logical (when do you know no contact matches?)
"No such contact"
}
// Only change code above this line
}
}
// Only change code below this line
for (var i = 0; i < contacts.length; i++) {
if (contacts[i].firstName == name) {
if (contacts[i].prop == undefined) { return "No such property" }
else{ return contacts[i].prop; }
}
else {return "No such contact" }
// Only change code above this line
}