Let’s break this problem down into smaller pieces.
So, the for loop iterates over the contacts array to see if any of the firstNames matches the name.
Now, if there is a firstName that matches name, the next step is checking if prop matches any property of the object that has the matching firstName. So, one of two things can happen here:
There is a property that has the same name as prop. In this case return the value of that property.
There isn’t any property that matches prop, in which case you return 'No such property'.
But hey, what if the for loop finished looping over the contacts array and didn’t find any matching firstNames? In that case, return 'No such contact'.
I hope this is helpful.
Let’s analyze why this one happens. The other two follow a similar logic.
So, the for loop reaches this object, which is the 3rd object in the contacts array(i===2).
Then, the if statement checks if contacts[i].firstName === name, and those 2 are indeed equal, so we proceed to the nested if statement.
The nested if statement checks if the contacts[i] object has the 'likes' property. And it does, so result gets the value ["Intriguing Cases", "Violin"].
Normally, the function should stop here. But it doesn’t.
There’s still one more object in the array, so the for loop keeps going to i===3. It does the first if statement again, which is false this time, so it jumps to the else branch, which says return "No such contact";. This is the first time the function finds a return statement, so it exits here and returns “No such contact”, which isn’t what you want.
Modify the function in such a way, that it stops immediately after it finds the answer you want. (Spoiler: You don’t really need that variable).
Hope i was helpful.
EDIT: After taking a second look, your function will exit right away if name doesn’t match the 'firstName' of the first object in the contacts array. return "No such contact"; only after you checked all the objects in contacts.