Hi, I’m redoing the JavaScript curriculum and got up to this challenge. For me the code is fine, I don’t understand why it is getting errors (not syntax errors, working errors). If someone can please help me here I’d be thankful.
// 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
if (name === "firstName" && name.hasOwnProperty(prop)) {
return prop;
} else if (!name.hasOwnProperty(prop)) {
return "No such property";
} else if (name = undefined) {
return "No such contact";
}
}
// Only change code above this line
lookUpProfile("Akira", "likes");
function lookUpProfile(name, prop){
// Only change code below this line
// Will the name ever actually be 'firstName'?
// Will the string have a property?
if (name === "firstName" && name.hasOwnProperty(prop)) {
return prop;
} else if (!name.hasOwnProperty(prop)) {
return "No such property";
} else if (name = undefined) { // This assigns 'undefined' to 'name'
return "No such contact";
}
}
I don’t think this syntax means what you think it does.
I’m not exactly sure why you are comparing name to the string "firstName" or checking if the string name has the property prop.
I think you are missing how to iterate over an array of objects in your 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 (i = 0; i < contacts.length; i++) {
if (contacts[i].firstName === name) {
if (contacts[i].hasOwnProperty(prop)) {
return contacts[i].name.prop
} else {
return "No such property"
}
}
}
return "No such contact"
// Only change code above this line
}
lookUpProfile("Akira", "likes");
Your browser information:
User Agent is: Mozilla/5.0 (Linux; Android 6.0.1; SM-G532M Build/MMB29T) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.91 Mobile Safari/537.36.
@JeremyLT I added var before i = 0 and replaced the dot notation with bracket notation. I tried with and without name. I keep getting the same errors. The most striking is I get an uncaught syntax error about regular expressions (?). Maybe some little spelling mistake we didn’t see? Thank you so much!