Basic JavaScript - Profile Lookup

I’m trying to do the “Profile Lookup”-challenge in the JSS course found here.

My code is passing all challenges except the “No such property”-challenge. I think I should have an else statement for that part, and I think I know where it goes (see the part commented out in my code below), but if I put it there, I get an error saying: “SyntaxError: unknown: Unexpected token (37:2)”, i.e. it says the “else” is an unexpected token? Having it there somehow also grays out the ‘return “No such contact”’-part.

Am I missing something obvious? Even looking at others’ solutions, it seems like using else here should be possible.

// Setup

const 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 (let i = 0; i < contacts.length; i++) {
    if (name === contacts[i].firstName && 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");

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/105.0.0.0 Safari/537.36

Challenge: Basic JavaScript - Profile Lookup

Link to the challenge:

An else needs to be associated with a if, not a for.

Thank you, Jeremy. I just realized else wasn’t associated with the if in the loop, and also that you can’t have an unconditional else within a loop without breaking the loop.

For any future readers, here is my rewrite that passes:


for (let i = 0; i < contacts.length; i++) {
    if (name === contacts[i].firstName && contacts[i].hasOwnProperty(prop)) {
        return contacts[i][prop];
    } else if (name === contacts[i].firstName && !contacts[i].hasOwnProperty(prop)) {
        return "No such property";
    }
}

return "No such contact";

1 Like