Profile Lookup difficulties

Profile Lookup difficulties
0

#1

Having a hard time with this one, I have looked through some of the other posts but still not seeing what I am missing. The first if statement runs fine until I put in the second else if statement then it stops working.

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

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

#2

i see you are missing a part of the code in the if statement [quote=“mbooth364, post:1, topic:70150”]
contacts[i].hasOwnProperty
[/quote]
there is a “(prop)” missing

it should looks like “contacts[i].hasOwnProperty(prop)”

And for the record this code run well in jsfiddle, maybe i don’t understand what do you mean with it is not working.


#3

Aside from the thing @caev03 pointed out:

When your function is executed, it starts the for loop. Now, on the first iteration, i = 0. You check the contacts[0] (which is an object) - if it’s .firstName matches your function argument and it has requested property, it returns that property’s value. Thats ok.
However, if contacts[0] does not have requested property, or if it’s firstName does not match provided name, it continues executing function. At this point it checks the second and third if statement and if any of them is true for contacts[0] then it RETURNS something. return keyword terminates the function. Because of the way that you have constructed your if statements, your function only checks the first object in the provided array.


#4

Ahh I see it now, thank you


#5

So what should I use instead of return?


#6

You have to use return, but in a different way.

Your problem is that your return breaks the loop. You have to write your loop in such a way, that it breaks only if you know, that there is no point in checking any other objects.

Think about that.
If firstName matches your first argument, and object you are checking at the moment has the property requested in a second argument - you are good to go. You can break the loop cause you have already found what you had been looking for.
If firstName matches your first argument, but the object you are checking at the moment does not have the property requested in a second argument - you can also break the loop. You have found the person, but he/she does not have the prop you are looking for.

However, if the firstName does not match your first argument - you can’t break the loop. You have to check all the objects. Which means that the return "No such contact" statement is at fault :wink:

Hope you can get to it now.


#7

I cleaned up your code.
You need to use triple backticks to post code to the forum.
See this post for details.