# What are the differences between these two functions?

**URL:** <https://forum.freecodecamp.org/t/what-are-the-differences-between-these-two-functions/41173>\
**Category:** General\
**Created:** [September 29, 2016, 9:37am UTC](https://forum.freecodecamp.org/t/what-are-the-differences-between-these-two-functions/41173 "2016-09-29T09:37:38Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![HannahCross](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/hannahcross/32/17407_2.png) [@HannahCross](https://forum.freecodecamp.org/u/HannahCross)\
**Post date:** [September 29, 2016, 9:37am UTC](https://forum.freecodecamp.org/t/what-are-the-differences-between-these-two-functions/41173/1 "2016-09-29T09:37:38Z")

</div>

Hi there!  
I was working on the profile lookup challenge (I will post the solution, don’t read if you haven’t completed it!).

I have two versions of the function, one works and one doesn’t, the only difference is in the order of the else statement “return no such contact”. I do not understand what is the difference between them.

This is the correct one:

function lookUpProfile(firstName, prop){  
// Only change code below this line  
for (i = 0; i \< contacts.length; i++) {  
if (contacts[i].firstName === firstName) {  
if (contacts[i].hasOwnProperty(prop)) {  
return contacts[i][prop];  
} else {  
return “No such property”;  
}  
}  
} return “No such contact”;

// Only change code above this line  
}

This does not work:

function lookUpProfile(firstName, prop){  
// Only change code below this line  
for (i = 0; i \< contacts.length; i++) {  
if (contacts[i].firstName === firstName) {  
if (contacts[i].hasOwnProperty(prop)) {  
return contacts[i][prop];  
} else {  
return “No such property”;  
}  
} else {  
return “No such contact”;  
}  
}  
// Only change code above this line  
}

---

<div class="post-metadata">

**Author:** ![nameToReachPeople](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/nametoreachpeople/32/10814_2.png) [@nameToReachPeople](https://forum.freecodecamp.org/u/nameToReachPeople)\
**Post date:** [September 29, 2016, 12:05pm UTC](https://forum.freecodecamp.org/t/what-are-the-differences-between-these-two-functions/41173/2 "2016-09-29T12:05:31Z")

</div>

Cause in first case u check whole contacts with ur desired name and only then when loop end  
by no one with same name found

`return 'no such contact'` occurs

But if something success in loop by meanimg returning control ( value ) to caller then line  
return ‘no such contact’ will not happen

P.s  
Just trace brackets

---

<div class="post-metadata">

**Author:** ![HannahCross](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/hannahcross/32/17407_2.png) [@HannahCross](https://forum.freecodecamp.org/u/HannahCross)\
**Post date:** [September 29, 2016, 2:44pm UTC](https://forum.freecodecamp.org/t/what-are-the-differences-between-these-two-functions/41173/3 "2016-09-29T14:44:09Z")

</div>

In the second case `return 'no such contact'` is inside the loop but should only be reached if the first conditional `if (contacts[i].firstName === firstName)` is not met. Then why is that wrong? Does being inside a loop affect it somehow?

---

<div class="post-metadata">

**Author:** ![kevcomedia](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/kevcomedia/32/125737_2.png) [@kevcomedia](https://forum.freecodecamp.org/u/kevcomedia)\
**Post date:** [September 29, 2016, 3:05pm UTC](https://forum.freecodecamp.org/t/what-are-the-differences-between-these-two-functions/41173/4 "2016-09-29T15:05:36Z")

</div>

Even so, inside the for-loop, all “paths” will lead to a `return`, effectively breaking your loop.

---

<div class="post-metadata">

**Author:** ![HannahCross](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/hannahcross/32/17407_2.png) [@HannahCross](https://forum.freecodecamp.org/u/HannahCross)\
**Post date:** [September 29, 2016, 6:55pm UTC](https://forum.freecodecamp.org/t/what-are-the-differences-between-these-two-functions/41173/5 "2016-09-29T18:55:59Z")

</div>

Oh! I get it now! Thank you @nameToReachPeople @kevcomedia ! You were very helpful.

---

<div class="post-metadata">

**Author:** ![jwilkins.oboe](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/jwilkins.oboe/32/179497_2.png) [@jwilkins.oboe](https://forum.freecodecamp.org/u/jwilkins.oboe)\
**Post date:** [January 16, 2021, 5:30am UTC](https://forum.freecodecamp.org/t/what-are-the-differences-between-these-two-functions/41173/6 "2021-01-16T05:30:07Z")

</div>

Closed on January 16, 2021, 5:30am UTC


