Skip to main content

--description--

When a return statement is reached, the execution of the current function stops and control returns to the calling location.

Example

function myFun() {
console.log("Hello");
return "World";
console.log("byebye")
}
myFun();

The above will display the string Hello in the console, and return the string World. The string byebye will never display in the console, because the function exits at the return statement.

--instructions--

Modify the function abTest so that if a or b are less than 0 the function will immediately exit with a value of undefined.

Hint
Remember that undefined is a keyword, not a string.

--hints--

abTest(2, 2) should return a number

assert(typeof abTest(2, 2) === 'number');

abTest(2, 2) should return 8

assert(abTest(2, 2) === 8);

abTest(-2, 2) should return undefined

assert(abTest(-2, 2) === undefined);

abTest(2, -2) should return undefined

assert(abTest(2, -2) === undefined);

abTest(2, 8) should return 18

assert(abTest(2, 8) === 18);

abTest(3, 3) should return 12

assert(abTest(3, 3) === 12);

abTest(0, 0) should return 0

assert(abTest(0, 0) === 0);

--seed--

--seed-contents--

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



// Only change code above this line

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

abTest(2,2);

--solutions--

function abTest(a, b) {
if(a < 0 || b < 0) {
return undefined;
}
return Math.round(Math.pow(Math.sqrt(a) + Math.sqrt(b), 2));
}