Hi,
I’m trying to solve the profile lookup problem without using the hasOwnProperty() method. I came up with this code but I don’t understand why it’s not working properly. Can anyone enlighten me as to why?
var i = 0;
while (i < contacts.length) {
if (((firstName == contacts[i].firstName) == true) && (contacts[i][prop] == false)) {
return "No such property";
}
else if (firstName == contacts[i].firstName) {
return contacts[i][prop];
}
i++;
}
return "No such contact";
Thanks!
Your if statement below is the culprit:
if (((firstName == contacts[i].firstName) == true) && (contacts[i][prop] == false)) {
In the test case of lookUpProfile(“Akira”, “address”) the following code
contacts[i][prop] == false
the contacts[i][prop] part is undefined because there is no property called “address”, so when you make the comparison undefined == false, it yields false.
On a side note, it is much better to use the strictly equal operator === vs the == operator for comparisons most of the time. Be aware of the following outcomes when making the same comparisons with each === and ==
true == 1 // true
true === 1 // false
0 == false // true
0 === false // false
'' == false // true
'' === false // false
'3' == 3 // true
'3' === 3 // false