Why does this code not work correctly if I don't place the last return outside of my loop?

Tell us what’s happening:

I did the Basic Javascript: Profile Lookup profile and has passed it but was wondering why this code I have pasted down below isn’t also a valid solution?

Here is the instructions for this lesson:
We have an array of objects representing different people in our contacts lists.

A lookUpProfile function that takes name and a property ( prop ) as arguments has been pre-written for you.

The function should check if name is an actual contact’s firstName and the given property ( prop ) is a property of that contact.

If both are true, then return the “value” of that property.

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

If prop does not correspond to any valid properties of a contact found to match name then return "No such property" .

I did struggle on this problem for a bit, and had to use the hints I posted the hints down below. However hint 3 confuses me and is what had me stumped. Why does my code not work if I don’t follow hint 3 instructions.
It doesn’t explain why either can someone please tell me why?

Hint 1

Use a for loop to cycle through the contacts list.

Hint 2

Use a nested if statement to first check if the firstName matches, and then checks if the prop matches.

Hint 3

Leave your return "No such contact" out of the for loop as a final catch-all.
freeCodeCamp.org](https://www.freecodecamp.org/learn)

Here is the wrong code that I was trying to write for the problem


// 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"]
}
];
//console.log(contacts[0]['firstName'])

function lookUpProfile(name, prop){
// Only change code below this line
for (var 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";
} else {
    return "No such contact";
}
}

// Only change code above this line
}

console.log(lookUpProfile("Kristian", "lastName"));

The correct code I wrote…

// 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"]
    }
];
//console.log(contacts[0]['firstName'])

function lookUpProfile(name, prop){
// Only change code below this line
for (var i = 0; i < contacts.length; i++) {
    if (name === contacts[i]['firstName'] && contacts[i].hasOwnProperty(prop)) {
        return contacts[i][prop];
    } else (name === contacts[i]['firstName'] && !contacts[i].hasOwnProperty(prop)) {
        return "No such property";
    }
}
return "No such contact"
// Only change code above this line
}

console.log(lookUpProfile("Kristian", "lastName"));

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.96 Safari/537.36.

Challenge: Profile Lookup

Link to the challenge:

Your function will stop doing anything and exit the very first time it encounters a return statement.

Okay thank you I still wasn’t getting it at first but I understand now that if I left the return statement inside the loop the function could end at the first iteration contacts[0] depending on the input arguments for the function. So lookUpProfile("Kristian", "lastName"); returns 'No such contact' because in the first iteration the inputs fail the first two test from my conditions but passes the last statement which is a catch all.
So it loop never iterates to contacts[3] which would contain the arguments inputted to the function because the function was called early. I don’t know why I could not wrap my mind around this at first but now I see that my solution I put at the bottom is also wrong because now the else statement is ending the function at the first iteration since it will pass that test. About to fix that real quick.

if you find the required value you can return it, there is no need to check other elements

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.