Profile look up problem - For loop

Tell us what’s happening:
I don’t really understand why “else { return “No such contact”; }” has to be after the for loop closing bracket, why can’t I put it inside the loop as long as it works properly?

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/83.0.4103.116 Safari/537.36.

Challenge: Profile Lookup

Link to the challenge:

1 Like

If it’s not, the loop will exit the first time a value is false.

…and you want the for loop to check for all contacts first

1 Like

I don’t really understand why “else { return “No such contact”; }”
else returns no such contacts because The first 2 statements fail
and the else will be executed. I am not sure why u start about this since when i try to run your code i get errors which i did comment further bellow:

Why can’t I put it inside the loop as long as it works properly?
Because: It won’t work properly yes u can put an else statement inside a for loop but the syntax of the loop itself will be invalid.

"Kristian", "lastName"

should return

"Vos"
"Sherlock", "likes"

should return

["Intriguing Cases", "Violin"]
"Harry","likes"

should return an array

This link will help u figure out where it goes: http://pythontutor.com/visualize.html#mode=edit