 # 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`":

``````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 -

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

## Solutions

Solution 1 (Click to Show/Hide)
``````// 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);
``````
7 Likes

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

37 Likes

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

7 Likes

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.

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

7 Likes

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

// 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));
}

25 Likes

// 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?

2 Likes

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

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.

4 Likes

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

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;`

5 Likes

After trying for a while, I finally got it to work.
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 <=.

1 Like

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

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);

Feeling sorry for myself not figuring this out without using the Hint button 7 Likes

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;

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.

// 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);

1 Like

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