Profile Lookup challenge: What am I doing wrong? :(

I cannot understand what am I doing wrong?
The error message is:

// running tests
lookUpProfile("Kristian", "lastName") should return "Vos"
lookUpProfile("Sherlock", "likes") should return ["Intriguing Cases", "Violin"]
lookUpProfile("Harry", "likes") should return an array
// tests completed

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){
// Only change code below this line
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";
    }
}
// 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/87.0.4280.66 Safari/537.36.

Challenge: Profile Lookup

Link to the challenge:

1 Like

Putting this inside your loop means the first name that does not match triggers this return statement.

Side note: your code formatting style for the {}s is a mix of two different conventions, which makes it harder for others to read your code.

Yes, I’ve solved it by adding it after the loop statement without else. Thank you very much
But actually, I didn’t understand why it happened like that. Because in the tasks it says that:

If name does not correspond to any contacts then return "No such contact"

so my code should look for the name (for example) “Kristian” into the names. It should find the name in the array and then it should look for the prop of it (“lastName” ) then give the value “Vos”

I really can’t see why it goes to else statement there?

The very first time your function encounters a return, the function stops. In this case, the first time you hit a name that did not match, the function stoped checking any other name. Nothing happens after return.

1 Like