Profile Lookup (JavaScript Challenege/Exercise) - Code explanation

Profile Lookup (JavaScript Challenege/Exercise) - Code explanation
0
#1

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:

0 Likes

#2

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

0 Likes

#3

Because of the way you have constructed your logic, if you leave out the break statements, then when you have iterated through all the objects in the array and increment i with:

i++

your code will enter the while statement for the last time and i will be equal to contacts.length, so your result will be “No such contact”. No matter if the first name matches, your code will always result in “No such contact”.

0 Likes

#4

You could change:

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

to

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

and only have one break, because if the first name matches, you know for sure the result should be assigned to something and then stop looping.

The last example of code can be shrunk to:

    if (contacts[i].firstName === firstName) {
      result = contacts[i].hasOwnProperty(prop) ? contacts[i][prop] : "No such property";
      break;
    }
0 Likes

#5

Thank you for your answer Kaytbode! Appreciate it!

0 Likes

#6

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.

0 Likes

#7

Yes, it can be solved with a for loop and if else statements:

function lookUpProfile(name, prop){
  for (var i = 0; i < contacts.length; i++) {
    if (contacts[i].firstName == name) {
      if (contacts[i].hasOwnProperty(prop)) {
        return contacts[i][prop];  
      }
      else {
        return "No such property";
      }
    }
  }
  return "No such contact"; // if we have gone through the entire array, then we know there is no such contact
}

or just:

function lookUpProfile(name, prop){
  for (var i = 0; i < contacts.length; i++) {
    if (contacts[i].firstName == name) {
      if (contacts[i].hasOwnProperty(prop)) {
        return contacts[i][prop];  
      }
      return "No such property"; // no need for else, because if the property existed, we would have already returned the value in if part.
    }
  }
  return "No such contact"; // if we have gone through the entire array, then we know there is no such contact
}

but still prefer the use of the ternary operater:

function lookUpProfile(name, prop){
  for (var i = 0; i < contacts.length; i++) {
    if (contacts[i].firstName == name) {
      return contacts[i].hasOwnProperty(prop) ? contacts[i][prop] : "No such property";
    }
  }
  return "No such contact"; // if we have gone through the entire array, then we know there is no such contact
}

See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Conditional_Operator for more information about the ternary operator.

0 Likes

#8

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!

0 Likes

#9

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

#10

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.

0 Likes