Profile lookup challenge -

Profile lookup challenge -
0

#1

Hi all,

I have met all off the requirements to pass the challenge, but there is one problem…
So, the first part where firstName == firstName in the array and property == property in the array only works by itself. Once I add “else if” or “else” to pass the second part, where the function returns “No such contact” and “No such property” the first “if” stops working, but the second the rest of the code works properly.

I am seriously stuck here.

Take a look:

//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 (firstName == contacts[i].firstName && Object(contacts[i]).hasOwnProperty(prop) ) {
     return contacts[i][prop];
    }
  else if (firstName != contacts[i].firstName) {
    return "No such contact";
  } 
  else if (Object.keys(contacts[i]).indexOf(prop) != prop) {
    return "No such property";   
    }
  }

  }

// Only change code above this line

// Change these values to test your function
lookUpProfile("Bob", "number");

#2

Let’s look at one of the test cases where your solution fails.

We will examine:

lookUpProfile(“Kristian”, “lastName”);

In the first iteration of the for loop, the first if statement (seen below) evaluates to false, because firstName == contacts[i].firstName evaluates to false because firstName is “Kristian” and contacts[0].firstName is “Akira” which are not equal. When the && operator is used, both conditions have to be true.

if (firstName == contacts[i].firstName && Object(contacts[i]).hasOwnProperty(prop) ) {

so next the following else if statement evaluates to true, from the previous if statement evaluation we already know that “Kristian” is not equal to “Akira”. Since they are not equal, the else if block of code is ran which returns “No such contact”.

else if (firstName != contacts[i].firstName) {

You should add some console.log statements to check the values of variables/properties before the series of if statements, so you can see what is being evaluated during each iteration of the for loop.