Help for Profile Lookup

Tell us what’s happening:
I cant seem to figure out why this is not working.
Can someone please help?

Your code so far


// 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) {
for (var i = 0; i < contacts.length; i++) {
    if (contacts[i].firstname === name) {
        if (contacts[i].hasOwnProperty(prop)) {
            return contacts[i][prop]
        }else {
            return "No such property"
        }
    }else {
        return "No such contact"
    }
}
}
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/84.0.4147.89 Safari/537.36.

Challenge: Profile Lookup

Link to the challenge:

This also wont work

function lookUpProfile(name, prop) {
    for (var i = 0; i < contacts.length; i++) {
        if (contacts[i].firstname === name) {
            return contacts[i][prop] || "No such property"
        }else return "No such contact"
    }
}
lookUpProfile("Akira", "likes");

This piece here will cause you problems. The very first time that your code encounters a ‘return’ the function execution stops. This means that the first time you encounter a name that does not match, you ‘return’ “No such contact”. You should only ‘return’ that statement when you have check all names.

Even with the else it will only return “No such contact”

because it returns prematurely

when can you be sure that there is no such contact? after looking just at first one? or after looking at all contacts?

1 Like