# freeCodeCamp Challenge Guide: Return Early Pattern for Functions

**URL:** <https://forum.freecodecamp.org/t/freecodecamp-challenge-guide-return-early-pattern-for-functions/18272>\
**Category:** freeCodeCamp Contributors\
**Tags:** guide\
**Created:** [July 7, 2016, 9:13pm UTC](https://forum.freecodecamp.org/t/freecodecamp-challenge-guide-return-early-pattern-for-functions/18272 "2016-07-07T21:13:50Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![camperbot](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/camperbot/32/18364_2.png) [@camperbot](https://forum.freecodecamp.org/u/camperbot)\
**Post date:** [July 7, 2016, 9:13pm UTC](https://forum.freecodecamp.org/t/freecodecamp-challenge-guide-return-early-pattern-for-functions/18272/1 "2016-07-07T21:13:51Z")

</div>

# [Return Early Pattern for Functions](https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/basic-javascript/return-early-pattern-for-functions)

* * *

## Hints

### Hint 1

We need to modify the function `abTest` so that if `a` or `b` are less than `0` the function will immediately exit with a value of `undefined`.

We add in body of function simple `if` statement, which, under the conditions “if `a` or `b` are less than `0` - immediately exit with a value of `undefined`”:

```javascript
if (a < 0 || b < 0) {
  return undefined;
}

```

### Hint 2

Now, if `a` or `b` are less than `0` - function exit with a value of `undefined`, in other cases -

```javascript
return Math.round(Math.pow(Math.sqrt(a) + Math.sqrt(b), 2));

```

* * *

## Solutions

> **Solution 1 (Click to Show/Hide)**
>
> ```javascript
> // Setup
> function abTest(a, b) {
> // Only change code below this line
> if (a < 0 || b < 0) {
> return undefined;
> }
> 
> // Only change code above this line
> 
> return Math.round(Math.pow(Math.sqrt(a) + Math.sqrt(b), 2));
> }
> 
> // Change values below to test your code
> abTest(2, 2);
> 
> ```

---

<div class="post-metadata">

**Author:** ![DRD161](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/drd161/32/61333_2.png) [@DRD161](https://forum.freecodecamp.org/u/DRD161)\
**Post date:** [July 2, 2017, 5:42pm UTC](https://forum.freecodecamp.org/t/freecodecamp-challenge-guide-return-early-pattern-for-functions/18272/2 "2017-07-02T17:42:17Z")

</div>

Horrible “Hint” It is the EXACT same thing on the other page!! Come on now.

---

<div class="post-metadata">

**Author:** ![gadalaavinash](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/gadalaavinash/32/161818_2.png) [@gadalaavinash](https://forum.freecodecamp.org/u/gadalaavinash)\
**Post date:** [July 3, 2017, 4:53am UTC](https://forum.freecodecamp.org/t/freecodecamp-challenge-guide-return-early-pattern-for-functions/18272/3 "2017-07-03T04:53:37Z")

</div>

HINT: Use if statement to check if the condition fails aka variable 1 less than 0 or variable 2 less than 0 then return undefined else success return

---

<div class="post-metadata">

**Author:** ![DRD161](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/drd161/32/61333_2.png) [@DRD161](https://forum.freecodecamp.org/u/DRD161)\
**Post date:** [July 3, 2017, 10:51am UTC](https://forum.freecodecamp.org/t/freecodecamp-challenge-guide-return-early-pattern-for-functions/18272/4 "2017-07-03T10:51:58Z")

</div>

I tried that and when I get to the else statement the return statement  
return Math.round(Math.pow(Math.sqrt(a) + Math.sqrt(b), 2)); does not match up with the scope of the else. When I try to add another return statement into the else statement the output is nothing.

---

<div class="post-metadata">

**Author:** ![jjmastermind](https://avatars.discourse-cdn.com/v4/letter/j/5f9b8f/32.png) [@jjmastermind](https://forum.freecodecamp.org/u/jjmastermind)\
**Post date:** [July 3, 2017, 11:50am UTC](https://forum.freecodecamp.org/t/freecodecamp-challenge-guide-return-early-pattern-for-functions/18272/5 "2017-07-03T11:50:45Z")

</div>

All you need is an if statement.  
for example  
if x === z || y ===z{  
return;  
}

---

<div class="post-metadata">

**Author:** ![MikeWallet](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/mikewallet/32/60341_2.png) [@MikeWallet](https://forum.freecodecamp.org/u/MikeWallet)\
**Post date:** [July 3, 2017, 4:45pm UTC](https://forum.freecodecamp.org/t/freecodecamp-challenge-guide-return-early-pattern-for-functions/18272/7 "2017-07-03T16:45:27Z")

</div>

Too bad they never explained this, took me quite some time to figure this1 out. The gitter chat helped met out eventually 😅

---

<div class="post-metadata">

**Author:** ![Ahmed-Elbessfy](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/ahmed-elbessfy/32/38737_2.png) [@Ahmed-Elbessfy](https://forum.freecodecamp.org/u/Ahmed-Elbessfy)\
**Post date:** [July 4, 2017, 4:15am UTC](https://forum.freecodecamp.org/t/freecodecamp-challenge-guide-return-early-pattern-for-functions/18272/8 "2017-07-04T04:15:24Z")

</div>

// Setup  
function abTest(a, b) {  
// Only change code below this line  
if (a \< 0 || b \< 0 ){  
return ;

}

// Only change code above this line

return Math.round(Math.pow(Math.sqrt(a) + Math.sqrt(b), 2));  
}

---

<div class="post-metadata">

**Author:** ![joeygerovac](https://avatars.discourse-cdn.com/v4/letter/j/7c8e57/32.png) [@joeygerovac](https://forum.freecodecamp.org/u/joeygerovac)\
**Post date:** [July 9, 2017, 2:21am UTC](https://forum.freecodecamp.org/t/freecodecamp-challenge-guide-return-early-pattern-for-functions/18272/9 "2017-07-09T02:21:56Z")

</div>

// Setup  
function abTest(a, b) {  
// Only change code below this line  
if (a \< 0 || b \< 0) {  
return “undefined”;  
}

// Only change code above this line

return Math.round(Math.pow(Math.sqrt(a) + Math.sqrt(b), 2));  
}  
// Change values below to test your code  
abTest(2,2);

I have this and it is still not working?

---

<div class="post-metadata">

**Author:** ![runawaycat](https://avatars.discourse-cdn.com/v4/letter/r/f07891/32.png) [@runawaycat](https://forum.freecodecamp.org/u/runawaycat)\
**Post date:** [July 9, 2017, 6:28pm UTC](https://forum.freecodecamp.org/t/freecodecamp-challenge-guide-return-early-pattern-for-functions/18272/10 "2017-07-09T18:28:20Z")

</div>

Hey, in case you haven’t solved it yet, return undefined; should go without quotes 😉

---

<div class="post-metadata">

**Author:** ![kapilsipl](https://avatars.discourse-cdn.com/v4/letter/k/ecae2f/32.png) [@kapilsipl](https://forum.freecodecamp.org/u/kapilsipl)\
**Post date:** [July 13, 2017, 8:33am UTC](https://forum.freecodecamp.org/t/freecodecamp-challenge-guide-return-early-pattern-for-functions/18272/11 "2017-07-13T08:33:23Z")

</div>

Correct code is,  
if(a \< 0 || b \< 0){  
return undefined;  
}  
Above code is defined return value when condition found true, undefined is JavaScript’s primitive types which does not required string sign to execute.

---

<div class="post-metadata">

**Author:** ![daksh844](https://avatars.discourse-cdn.com/v4/letter/d/ecb155/32.png) [@daksh844](https://forum.freecodecamp.org/u/daksh844)\
**Post date:** [July 16, 2017, 9:10am UTC](https://forum.freecodecamp.org/t/freecodecamp-challenge-guide-return-early-pattern-for-functions/18272/12 "2017-07-16T09:10:28Z")

</div>

Here is the correct answer:  
if(a \< 0 || b \< 0){  
return undefined;  
}

---

<div class="post-metadata">

**Author:** ![js101](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/js101/32/61587_2.png) [@js101](https://forum.freecodecamp.org/u/js101)\
**Post date:** [July 18, 2017, 9:28am UTC](https://forum.freecodecamp.org/t/freecodecamp-challenge-guide-return-early-pattern-for-functions/18272/13 "2017-07-18T09:28:19Z")

</div>

not sure which is correct - **what do you guys think?** (both _solve the exercise_):

- return undefined;  
OR
- return;

I guess it’s the second one - just `return;`

---

<div class="post-metadata">

**Author:** ![Keronmat](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/keronmat/32/54617_2.png) [@Keronmat](https://forum.freecodecamp.org/u/Keronmat)\
**Post date:** [July 22, 2017, 3:14pm UTC](https://forum.freecodecamp.org/t/freecodecamp-challenge-guide-return-early-pattern-for-functions/18272/14 "2017-07-22T15:14:35Z")

</div>

After trying for a while, I finally got it to work.  
FIRST i had:  
IF (a\<0 || b\< 0){  
return;  
}  
This didn’t worked, but then I did this:  
IF (a\<=0 || b\< =0){  
return;  
}  
Then it worked, the only difference being \<=.

---

<div class="post-metadata">

**Author:** ![DevinderThakur](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/devinderthakur/32/57495_2.png) [@DevinderThakur](https://forum.freecodecamp.org/u/DevinderThakur)\
**Post date:** [July 23, 2017, 1:38pm UTC](https://forum.freecodecamp.org/t/freecodecamp-challenge-guide-return-early-pattern-for-functions/18272/15 "2017-07-23T13:38:38Z")

</div>

what is immediately exit with a value of undefined.  
i clear the quiz but it is not returning anything when a or b is less than 0

---

<div class="post-metadata">

**Author:** ![studentsa](https://avatars.discourse-cdn.com/v4/letter/s/73ab20/32.png) [@studentsa](https://forum.freecodecamp.org/u/studentsa)\
**Post date:** [July 24, 2017, 6:19pm UTC](https://forum.freecodecamp.org/t/freecodecamp-challenge-guide-return-early-pattern-for-functions/18272/16 "2017-07-24T18:19:51Z")

</div>

The below also works using && instead of ||

// Setup  
function abTest(a, b) {  
// Only change code below this line

if(a \> 0 && b\> 0) {}  
else {return undefined;}

// Only change code above this line

return Math.round(Math.pow(Math.sqrt(a) + Math.sqrt(b), 2));  
}

// Change values below to test your code  
abTest(2,2);

---

<div class="post-metadata">

**Author:** ![KBPsystem777](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/kbpsystem777/32/61555_2.png) [@KBPsystem777](https://forum.freecodecamp.org/u/KBPsystem777)\
**Post date:** [July 25, 2017, 12:27pm UTC](https://forum.freecodecamp.org/t/freecodecamp-challenge-guide-return-early-pattern-for-functions/18272/17 "2017-07-25T12:27:20Z")

</div>

Feeling sorry for myself not figuring this out without using the **Hint** button 😂

---

<div class="post-metadata">

**Author:** ![ignacio-rsg](https://avatars.discourse-cdn.com/v4/letter/i/9dc877/32.png) [@ignacio-rsg](https://forum.freecodecamp.org/u/ignacio-rsg)\
**Post date:** [July 26, 2017, 2:51pm UTC](https://forum.freecodecamp.org/t/freecodecamp-challenge-guide-return-early-pattern-for-functions/18272/18 "2017-07-26T14:51:52Z")

</div>

for me using && did not work, and going to the detail the instructions say “Modify the function abTest so that if a **or** b are less than 0 the function will immediately exit with a value of undefined.”

So I would suggest to use || instead of &&.

if (a \< 0 || b \< 0)  
return undefined;

---

<div class="post-metadata">

**Author:** ![studentsa](https://avatars.discourse-cdn.com/v4/letter/s/73ab20/32.png) [@studentsa](https://forum.freecodecamp.org/u/studentsa)\
**Post date:** [July 26, 2017, 5:02pm UTC](https://forum.freecodecamp.org/t/freecodecamp-challenge-guide-return-early-pattern-for-functions/18272/19 "2017-07-26T17:02:37Z")

</div>

Even though && is saying “and” rather than “or” it accomplishes the same goal/means the same thing at the end of the day. Think of it this way: In order for (a \> 0 && b\> 0) to evaluate as true, it means BOTH a and b must be greater than zero – thus if one of those is less than zero, then the function will immediately exit with a value of undefined. You can almost always create and && that will produce the same result as an || and vica versa.

---

<div class="post-metadata">

**Author:** ![dhruvjadvani](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/dhruvjadvani/32/63383_2.png) [@dhruvjadvani](https://forum.freecodecamp.org/u/dhruvjadvani)\
**Post date:** [July 27, 2017, 5:02am UTC](https://forum.freecodecamp.org/t/freecodecamp-challenge-guide-return-early-pattern-for-functions/18272/20 "2017-07-27T05:02:16Z")

</div>

// Setup  
function abTest(a, b) {  
// Only change code below this line  
if(a\<0||b\<0){  
console.log(undefined);  
return undefined;}

// Only change code above this line

return Math.round(Math.pow(Math.sqrt(a) + Math.sqrt(b), 2));  
}

// Change values below to test your code  
abTest(2,2);

---

<div class="post-metadata">

**Author:** ![mahedyhasan](https://avatars.discourse-cdn.com/v4/letter/m/13edae/32.png) [@mahedyhasan](https://forum.freecodecamp.org/u/mahedyhasan)\
**Post date:** [July 29, 2017, 5:08pm UTC](https://forum.freecodecamp.org/t/freecodecamp-challenge-guide-return-early-pattern-for-functions/18272/21 "2017-07-29T17:08:01Z")

</div>

look at the given hint what it say!.undefined is a KEYWORD!.

[Next page](https://forum.freecodecamp.org/t/freecodecamp-challenge-guide-return-early-pattern-for-functions/18272.md?page=2)
