Profile Lookup || Why doesn't this work?

Profile Lookup || Why doesn't this work?
0

#1

//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){
var result;

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

}else {
  return "No such contact";
}

}

// Only change code above this line
}

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

In the above, every condition passes except for “No such contact”; - I’ve been at this for ages now and finally decided to just ask for help.

Thanks


#2

A few things you need to change.

  • you need to open some curly braces after your first if statement.
  • you don’t need to check for the hasOwnProperty(prop) === false. When you reach that point, you already tested on the if the same thing. If it is not true, you fall on the else statement anyway. No need to test again.
  • finally, the return "No such contact";, needs to be done outside the for loop. Only after looping through all the names, you can be sure that the contact does not exist. If the name exists, the function returns something else and finishes execution before reaching this point.

So, now you can try implementing those things on your own. Of you’re totally confused here’s your code with those fixes:

Spoiler
function lookUpProfile(firstName, prop){
var result;

for (var i = 0; i < contacts.length; i++) {
  if (contacts[i].firstName === firstName){ // <-- here you need to open curly braces
    if (contacts[i].hasOwnProperty(prop)) {
      return contacts[i][prop];
    } else{ // <- you dont need to check again the value of contacts[i].hasOwnProperty(prop),
              // if not true we do the else, which means it was false
    return "No such property";
    }}} //Here close all ifs and the for loop
  return "No such contact"; //only after looping through everything and not finding anything, you can safely
//say there is no such contact.
}

#3

`Really appreciate that zelite - i’ve gone back over IF statements to get a better understanding. Thanks!


#4

Why can’t the return "No such property" be inside an else statement. Why doesn’t is work? The second if statement has an else statement but no else statement for the first one.


#5

If you are referring to @zelite’s solution, then the reason an else is not needed for the first if statement is because the first one is just checking if the current objects firstName property is equal to the firstName argument. If it is false, then nothing should happen (no returns) and then the for loop iterates to the next object.