Profile Lookup help

Can someone tell me what I am doing wrong here? The no such property or contact part works.

function lookUpProfile(firstName, prop){
// Only change code below this line

for (i = 0; i < contacts.length; i++) {
if (firstName == contacts[i].firstName) {
if (prop == contacts[i][prop]) {
return contacts[i][prop];

} else {
return “No such property”;
}
} else {
return “No such contact”;
}
}

// Only change code above this line
}

Hey, you are close. Have a look at this line: if (prop == contacts[i][prop]) {. You are checking if prop (which is the name of a property) is equal to `contacts[i][prop] (which is the value of that property. So you are comparing the name with the value, which will return false. You should check if that property exists.

What BenGitter says. And…

{property: John},
(property: Dave},
(property: Gil},

target value = Jenny
property value = John or something

if the target value does not equal to the property value, doesn’t mean that the target does not exist.
if Jenny =/= John, then that doesn’t mean that I proved Jenny exist.
If I would ctrl-f then type adslfkjd then adslfkjd does not exist; because the browser search for every single occurances then return if it exist or not.

that’s the best hint that i can come up with without giving much. idk. Hope you can get something out of this.

You should also think about where you should return "No such contact"; in your code.

I finally finished this one. I changed the if (prop == contacts[i][prop]) to if (contacts[i].hasOwnProperty(prop) === true ).

It did not work until I took out the last
else {
return “No such contact”;
}

So this is the final code.

for (i = 0; i < contacts.length; i++) {

if (firstName == contacts[i].firstName) { 
  
  if (contacts[i].hasOwnProperty(prop) === true ) {
   
  return contacts[i][prop];

} else {

  return "No such property";

}

}

}
return “No such contact”;

Thanks a lot for the help.

Can anyone explain how this work?

assume var contacts =[ {firstName: a0, prop:b0}, {firstName:a1, prop:b1}, {firstName:a2, prop:b2}

for (i = 0; i < contacts.length; i++) {

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

if (prop == contacts[i].prop) {
return contacts[i].prop;

} else {
return “No such property”;
}

}

else {
return “No such contact”;
}

sorry missing ]

var contacts =[ {firstName: a0, prop:b0}, {firstName:a1, prop:b1}, {firstName:a2, prop:b2}]