Profile lookup challenge- why this code works?

hello I’ve been stuck for about 2 hours on the Profile Lookup challenge, and i managed to solve it but it was a lucky guess actually so i don’t why this code works: https://jsfiddle.net/8payutyy/

and this one doesn’t https://jsfiddle.net/58v7sg9h/

when i apply the second solution it says No such contact even for the existed ones.

i don’t know why the first one worked and the second one didn’t.

1 Like

Have a look at the loop in the example that isn’t working. You are returning from the function from within the loop. So the first time you have a contact where the firstName doesn’t match, you return ‘No such contact.’ and never check the remaining elements. Likewise for the property check in both examples, actually.

Without writing the code …
First you need to establish that contact is the one you want. If so, you check to see if the property exists. Based on that check, you return the property or no such property exists. If you go through all of the contacts without a match, then you return no such contact.

Hope that helps without giving too much away!

1 Like

Thanks alot that is really helpful it is clear now.