Profile Lookup [Spoilers] [Solved]

same problem here… did you find out any answer?

Here’s a solution using a WHILE statement:
function lookUpProfile(firstName, prop) {
// Only change code below this line

var i=0;
while (i < contacts.length) {

if (firstName == contacts[i].firstName) {
if (contacts[i].hasOwnProperty(prop)) {
return contacts[i][prop];
}
else {
return “No such property”;
}
}
i++;
} //end whileloop

    return "No such contact";

// Only change code above this line
}

This also took me a couple days, but I finally settled on a good solution. I would love to hear if there is a way to make this better though. My logic is as follows:

function lookUpProfile(firstName, prop){
// Only change code below this line
for (var i = 0; i < contacts.length; i++) {
  if (contacts[i].firstName == firstName) {
    if (contacts[i].hasOwnProperty(prop) === true) {
      return contacts[i][prop];
   }

I heard before it is always good practice when using and if-else chain, to first check for the most least likely match, so that if your condition fails you can loosen the conditions until you are left with an answer. So my first check was to see if firstName AND prop are in each object.

Note: that instead of using multiple conditions in one if statement, I split them into two if statements, so that if its not an exact match we know WHY (weather firstName or prop failed).

Next:

     else {
       return "No such property";
     } 
   } //end of outer if statement for name check
 } //end of for loop
return "No such contact"; //default return if for loop fails

Here is where I kept getting caught up and where I saw others having similar issues. The tricky part is, the question requires 3 return statements for each possible outcome, but they cant all be inside of the for loop (or else you wouldn’t be able to iterate over all the objects since a return is guaranteed to execute on the first iteration). So you have to pick one that the function runs by default if the for loop finishes without any if the if statements executing.

// Only change code above this line
} //end of function

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

So the whole purpose of my for loop is to first check if firstName is in any of the objects, then check if the prop is in that same object (during same iteration). If yes, return contacts[i][prop] (that’s one outcome). If not, return “No such property”, that’s another outcome. Then close the for loop with a curly bracket, and OUTSIDE the for loop on the next line, place your final outcome check (return “No such contact”). This needs to be outside because if the for loop finishes without any of the first return statements executing, the only possible outcome is the one that hasn’t been used yet. So it’s almost like the first two conditions are being used as a “break” statement for the whole function, and if there is no break just return your default outcome.

This was a nice experience for practicing your logic and algorithmic capabilities. It’s important to know why you are taking each step and its consequences. Taking a look at the previous answers and trying to understand them really helped a lot, and no you shouldn’t feel guilty for peeking at other peoples work, given that you put in a genuine effort yourself, got stuck, and wanted to gain some insight as opposed to just copy and paste to get it over with, that’s part of learning! Anyways let me know if I can help anyone else!

1 Like

Thanks for this solution! My own solution worked, but this is way more elegant. It’s good to be able to look up how it should have been done.

My solution was so close to this and I couldn’t understand why it wasn’t working. It was as simple as me having [] around the .hasOwnProperty prop instead of (). Thanks!