inside your loop at least one return statement will always execute, it means that the loop will stop at i = 0, and check only the first element in the array, never the others
you can use the tool pythontutor (find it on google!) to see how your code execute
function lookUpProfile(name, prop){
// Only change code below this line
for (var x = 0; x < contacts.length; x++) {
if (name == contacts[x].firstName) {
if (contacts[x].hasOwnProperty(prop)) {
return contacts[x][prop];
}
return "No such property";
}
}
return "No such contact"
// Only change code above this line
}
I don’t understand why last return statement need to be there
function lookUpProfile(name, prop){
// Only change code below this line
for (var x = 0; x < contacts.length; x++) {
if (name == contacts[x].firstName) {
if (contacts[x].hasOwnProperty(prop)) {
return contacts[x][prop];
}
return "No such property";
}
return "No such contact"
}
// Only change code above this line
}
Ok I think I get it. If the function looked like this:
function lookUpProfile(name, prop){
// Only change code below this line
for (var x = 0; x < contacts.length; x++) {
if (name == contacts[x].firstName) {
if (contacts[x].hasOwnProperty(prop)) {
return contacts[x][prop];
}
return "No such property";
}
return "No such contact"
}
// Only change code above this line
}
We won’t let function to cycle through all of contacts elements.