Return Early Pattern for Functions//

Tell us what’s happening:
I am trying to complete this challenge by following the instructions which are as follows:
" Modify the function abTest so that if a or b are less than 0 the function will immediately exit with a value of undefined .
I thought that was simple enough so I solved it like shown in the code. However this lets me pass only two of the test parameters:
-abTest(-2,2) should return undefined
-abTest(2,-2) should return undefined
which it does. But the test asks for all of the parameters to be met in order to be able to proceed to the next one. Those parameters are as follows:
-abTest(2,2) should return a number
-abTest(2,2) should return 8

  • abTest(2,8) should return 18
    -abTest(3,3) should return 12
    -abTest(0,0) should return 0
    How can I modify the function in a way that all of those can evaluate to the appropriate return values at the same time?
    Any kind of help would be greatly appreciated!
    Your code so far
       // 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));
}
console.log(abTest(2,2));

// 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));
}
console.log(abTest(2,2));

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.61 Safari/537.36.

Challenge: Return Early Pattern for Functions

Link to the challenge:

1 Like

Get rid of that semi-colon after your if statement and you should pass. :slight_smile:

1 Like

maaaan!!! Thank you so much, it worked. But I thought that semicolons are just to serve as indications for the end of lines and are mostly ignored by JS?
Can you explain please?

They do indicate the end of an expression.
But think about where you put it:
if (condition); means your if statement doesn’t run a function.
if (condition) {do this}; would work.

((line !== expression) thanks ariel!)

1 Like

A semicolon indicates the end of an expression. When you put it between the condition and the code block of an if statement, you are putting it in the middle of an expression, making it incomplete.

3 Likes

Thanks again. I am just starting with this and sometimes a good explanation is all one needs.
Cheers!

Thank you! That makes sence:)

Happy coding!