After completing an exercise, I found the correct solution below. However, my initial solution did not work and I'm trying to understand why.
The only difference was in this line of code:
for (var i = contacts.length; i > 0; i--) {
I am curious as to why reversing the direction did not produce the same result?
for (var i = 0; i < contacts.length; i++) {
if (contacts[i].firstName == name){
if (contacts[i].hasOwnProperty(prop)){
return contacts[i][prop];
} else{
return "No such property";
}
}
}
return "No such contact";