Problem iterating through data in Profile Lookup

Hi.
I’am working on the JavaScript basics and I got stuck at the “Profile Lookup”.
It looks like my for loop does not iterate through the data. If I want to look up for someone other that Akira I only get the “No such contact” output. Can somebody please point out what the problem is here? Thank you.

//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"]
    },
    {
        "firstName": "Christiano",
        "lastName": "Vozzo",
        "number": "unknown",
        "likes": ["Javascript", "GangBang", "Foxes"]
    }
];

function lookUpProfile(firstName, prop){


 for(i = 0; i < contacts.length; i++){
      if(contacts[i].firstName === firstName && contacts[i].hasOwnProperty(prop) === true){        
        return contacts[i][prop];       
      }      
      else if(contacts[i].firstName !== firstName){
        return "No such contact";
      }      
      else if(contacts[i].hasOwnProperty(prop) === false){
        return "No such property";
      }   
      else{
        return "Why don't you iterate?";
      }          
   }
}

// Change these values to test your function
lookUpProfile("Kristian", "lastName");
lookUpProfile("Akira", "likes");
lookUpProfile("Akira", "lastName");
lookUpProfile("Kristian", "lastName");

What happens when you call return anywhere in a function (any function)?

2 Likes

I see a couple of immediate things … You don’t declare i before using it in your for loop, and you don’t have to “check” the truthy-ness of .hasOwnProperty(). By definition, it returns true or false (see MDN). For example:

for (var i = 0; i < contacts.length; i++) {
    if (contacts[i].firstName === firstName && contacts[i].hasOwnProperty(prop) ){
        return contacts[i][prop];
    } else if // etc etc etc   

That said, my solution used nested conditionals instead of logically && multiple conditions together.

Ah, I misread your issue. @jenovs rhetorical question is the key…

Hi. Thanks both for replying.

@jenovs
I think I know what you are saying. I’ll try it out as soon as I get back to my problem.

@belcurv
I not sure I understand what you mean with the truthy-ness part?

MDN’s description of Truthy with examples: link.

The conditional if statement checks the Truthy-ness of the expression within the parentheses. That expression needs to evaluate to Truthy for the code block to execute. It can either be Truthy or equal true. You’ll get more experience with this when you get to the FCC Falsy Bouncer challenge.

hasOwnProperty() is a method (a function belonging to an object) that returns true or false by itself. You don’t need to check if it’s result is true. That’s like checking if true is true. Of course, true is true … so the if statement’s condition will pass, but this extra equality check is unnecessary.

Thanks again both for taking your time and helping me out. I’ve managed to sort this out today :slight_smile: Cheers!