Profile Lookup - can't seem to get it [SOLVED]

Profile Lookup - can't seem to get it [SOLVED]
0

#1

Struggling to understand the Profile Lookup challenge.


//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) {
    if (contacts[i].hasOwnProperty(prop)) {
      return contacts[i][prop];
  }
  else {
    return "No such property";
  }
  }
    return "No such contact";
}
// Only change code above this line
}

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

This is how I’m understanding how I wrote my code. It checks if the name is in the array If yes, then it checks if it has that property. If yes to the 2nd condition, then it’ll return the value of the property. If false to the 2nd condition, it’ll return “No such property”.

If right away, there is no first name in the array, then it should say “No such contact”.

I can pass the challenge if I move the close bracket above the last return statement (essentially closing out the for loop), but then I don’t understand how that works, but my current bracket placement doesn’t.


#2

Yes, the return “No such contact”; should be outside the for loop. You want to do that after you’ve inspected every single item, not just he first.

I’m not sure what you’re not understanding. Understand that the return doesn’t return out the the for loop but exits the function completely. You don’t want to do that unless you find a match (the first return) or after you checked everything and not found a match (outside the for loop).

Perhaps with proper indenting it is clearer:

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

Be sure to always indent properly. And I like to label the ends of long code blocks so I don’t get lost.

If you’re still unsure how this is working, please ask again.


#3

@ksjazzguitar Thanks for explaining. I think I get it now. If the return “No such contact” is in the loop, then when contacts[0].firstName doesn’t equal Harry (or another name), it’ll automatically say “No such contact” rather than continuing to cycle through the loop. It won’t increment i and keep testing the other elements of the array.