JavaScript Profile Lookup (searching in arrays)

Could someone nudge me in the right direction here? I’ve been staring at this problem for 2 days. I’ve changed many things, I’ve reassured myself about every part. I don’t get it. I just can’t think of a reason why it wouldn’t work. I would appreciate it if you wouldn’t just flat out give me the straight answer, but maybe drop small hints until I am able to crack it.

Here’s the instructions.

We have an array of objects representing different people in our contacts lists.

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

The function should check if firstName 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.

Here’s the code.


//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(firstName, prop){
// Only change code below this line
  for (var i = 0; i < contacts.length; i++) {
      if (contacts[i].firstName == firstName && contacts[i].hasOwnProperty(prop)) {
        return contacts[i].prop;
      }
      else {
        return "whats up";
      }
  }
// Only change code above this line
}

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

Also, does anyone know of a way to practice these kind of problems? Because I find myself getting stuck at this “searching through arrays” thing. Not the first time. Does getting good at algorithms help?

Thank you.

@Sandris maybe if and else combination don’t work semantically

Step through your code for each iteration of the for loop.

If the first contact is not “Kristian”, what happens?

ohhhhhhhhhhhhh. so if I understand correctly, that else { return "whats up" }; was ruining everything?
I put that there so innocently, for no reason at all to be honest.

The if statement says basically that if you find whatever you need there, we return the value of the property you want and that’s it.

However, if you DON’T find what you need there on the first try, we return “what’s up” and we’re done with the for loop and not running it anymore. Because return always stops the loop, no?

The loop went like “okay. checking if the firstName key’s value is “Kristian”. Nope. We have “Akira” here, so it’s a no go. We can’t return what he wanted to return in this case. Let’s go forward. Ah, now we’re at the else statement, and it seems that we can return “whats up”. Done. No more loops.”

Is my logic correct here?

Yeah, your logic seems correct to me! The else statement can be a trap (like in this case) because this if else conditional will be guaranteed to only run once given by where the return statements are positioned.

Basically yes but return does not end the for loop directly per se, it ends the function execution and as a secondary result, your for loops ends.