I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.
You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.
function lookUpProfile(name, prop) {
for (var i = 0; i < contacts.length; i = i + 1) {
{ // Misplaced brace here
if (contacts[i].hasOwnProperty(prop) && contacts[i].firstName == name) {
return contacts[i][prop];
} else if (contacts[i].firstName !== name)
return "No such contact" // Have you checked every contact at this point?
} // Misplaced brace here
else if (!contacts[i].hasOwnProperty(prop) && contacts[i].firstName == name ) {
return "No such property"
} // Extra brace here?
}
}
}
I tried to format this to something that others can read. I think you have some serious oganizational issues. It is difficult for me to understand what your code is doing due to the misplaced {}s and whitespace, and i think it is half of the issues you are seeing.
You should use your braces and whitespace to help you structure your logic.
for (initialize; condition; iterate) {
// Loop body goes here
}
and
if (condition 1) {
// Code here
} else if (condition 2) {
// Code here
} else {
// Code here
}
function lookUpProfile(name, prop)
{
for (var i = 0; i < contacts.length; i = i + 1 ) {
if (contacts[i].hasOwnProperty(prop) && contacts[i].firstName == name) {
return contacts[i][prop];
}
else if (!contacts[i].hasOwnProperty(prop) && contacts[i].firstName == name ) {
return "No such property" }
}
return "No such contact";
}
lookUpProfile("Akira", "likes");
I’d still fix the code formatting in the future; the formatting helps with clarity, readability, and debugability. All of those are key in coding jobs, especially when working on a team with others!