Profile Lookup (JavaScript Challenege/Exercise) - Code explanation

Hello All,

I solved the profile lookup challenge in the JavaScript section but I don’t understand my solution fully. Specifically, why does my code work only when I include the break statements? Any help would be greatly appreciated. Below is my 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
var result = “”;
var i = 0;

while (i < contacts.length + 1) {

if (i == contacts.length) {
  result = "No such contact";
  
}

else {
  if (i < contacts.length) {
    if (contacts[i].firstName === firstName) {
      if (contacts[i].hasOwnProperty(prop)) {
        result = contacts[i][prop];
        break;
      } else {
        result = "No such property";
        break;
      }
    }
  }
}
i++;

}

return result;

// Only change code above this line
}

// Change these values to test your function
lookUpProfile(“Harry”, “number”);

Thank you very much fellow campers! :slight_smile:

applying the ‘break’ takes you out of the loop.
the loop counter is still on… and your function will return 'No such property’
if your search is not the last item in the array

Thank you for your answer Kaytbode! Appreciate it!

Wow, thank you for your answer! You just taught me something new. The code with the one break statement is nice, and the last snippet code looks even better (but I understand the second block better but hopefully I’ll understand the third block of code soon!).

Can you solve the profile lookup question with a for loop and if/else statements? I tried it but my code never passed all of the tests. sad face.

I see; that is interesting. My early attempts looked like this:

function lookUpProfile(firstName, prop){
var result = “”;
for (var i = 0; i < contacts.length + 1; i++) {
if (i < contacts.length) {
if (contacts[i].firstName === firstName){
if (contacts[i].hasOwnProperty(prop)) {
result = contacts[i][prop];
} else {
result = “No such property”;
}
}
} else {
result = “No such contact”;
}
}
return result;
}

This code did not pass all tests - it only passed the “No such contact” test case. What mistake am I making here? I’m sorry if you have already asnwered my question in your previous response!

Your for loop version has the exact same problem your while loop had without the break statements. It will always show the very last value of result (which in this case is “No such contact”).

Below, I have re-written your for loop solution, so you can see how you would do it with as little code changed as possible:

function lookUpProfile(firstName, prop){
  var result = "";
  for (var i = 0; i < contacts.length; i++) {
    if (contacts[i].firstName === firstName){
      if (contacts[i].hasOwnProperty(prop)) {
        result = contacts[i][prop];
      } 
      else {
        result = "No such property";
      }
    }
  }
  if (result === "") { 
    result = "No such contact";
  }
  return result;
}

FYI - In the future, please see (Forum Code Formatting) under Code Blocks for how to format your code when posting. It really helps those of us who want to help to not have to reformat it on our own to begin to assist.

2 Likes

Thank you! That was super duper helpful. And yes, from now on I shall reformat any code that I post! Once again, thank you tons!

Cheers.