Remember, as soon as a return statement is executed, the function exits. Within the first iteration of the contacts array, you are returning something, so after the return statement is executed, the function exits and does not come back to finish iterating the for loop. Think about when you really would know for sure, the contact does not exist when returning “No such contact”.
I am not going to give you the answer, but I can point you in the right direction. When on Python Tutor change the values to “Sherlock”,“Likes” and see what happens. The program is returning to early.
What’s happening now is ------> because “i” is still at zero and does not find a name Sherlock in the zero index of the contact array it returns “No such contact”.
But the name "Sherlock does exist it’s just on a different index.
Ok so help me understand something. I wrote a simple code to test this:
function lookUpProfile(name, prop){
// Only change code below this line
for (var i = 0; i < contacts.length; i++) {
if (name == contacts[i].firstName && prop == contacts[i].hasOwnProperty([prop])) {
return contacts[i][prop];
}
else {
return "No points";
}
}
// Only change code above this line
}
Now this code should return “contacts[i][prop]” but when i test it it returns “No points”. Why?
Consider this, compare this with your code, in the sample each alphabet has all the key values as in the challenge example,
Now let’s go into your code,
Let’s say alphabet D ( at index 3 ) has both name and prop as true, so when the loop goes to D, it will output both name and prop value
And the parameter passed to the function are the values of D,
But when the loop starts at index 0, it doesn’t match any requirements for D, therefore the loop exits with the output as “No points”, but the expected output is the value of name and prop of D
Yep, i’m just little snoozy, hasOwnProperty() takes a prop as argument, but you have an array there, but in your first post, you have it correct and when i run it, its working fine for the elements that don’t match, but it doesn’t work for inputs that match
prop is a string and contacts[i].hasOwnProperty(prop) returns a boolean value of true or false, so they will not be equal. Unless prop is “true” or “false” string values, the comparison will evaluate to false.