Profile look up exercise

My code is bellow. I was struggling with this one. I tried few different ways, and then I checked in solutions and there was written with nested if…else, which didn’t make sense at all for me… ._. Could someone help me out. I really want to understand nested if…else like in solution and why my code doesn’t work, before I move forward…

// 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  &&  contacts[i][prop] === true) {
return contacts[i][prop];
} else if (contacts[i].firstName != name) {
return "No such contact"; 
} else if (contacts[i][prop] === false) {
return "No such property";
// 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/89.0.4389.82 Safari/537.36.

Challenge: Profile Lookup

Link to the challenge:

I not so long ago did the javascript section so this is what i think:

if you do it like it in the solution , in the first if statement it looks if the name in the objects of the array is the same as your name parameter. If not it does nothing and the for loop makes a repeat. If it is the same THEN it move on to the second IF statement to look if your prop existst.

What is important is that as soon as you use RETURN , your loop stops immediately! Your code uses return as a result for every statement which makes your loop stop at the first index of the array

Okayyy, so like in solution when there’s 2 nested IF which checks only if name and prop are TRUE and if they are prints that prop value and then loop stops? Right? So we don’t actually need to ask to check separately for FALSE conditions like I did, because when it loops through first time and doesn’t meet both TRUE conditions, you just tell what to print when one or other condition is not met? I think it’s getting clearer and why in the solution 2 IF statements are nested, so it because if one or other isn’t TRUE we can ask 2 different RETURN to print?

But then in second IF is contacts[x].hasOwnProperty(prop) . I understand what it does, but why we take hasOwnProperty, because it’s not defined anywhere so how console knows what is it.

Thank you for your answer! :bowing_woman:

hasOwnProperty is a standard method. It doesn’t need to be defined if that is what you mean.

a method is a property of an object which has a function that is defined in the background of javascript.

1 Like

it is defined, it is part of the JavaScript language

1 Like

Ohh… I see now… Thank you a lot for your help! It was very helpful! :relaxed:

1 Like