Basic JavaScript - Profile Lookup

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