I was stuck on the Basic JavaScript exercise: Return Early Pattern for Functions. The goal is to get the function to immediately return undefined and stop executing if variable a or b are less than 0. The function is abTest(a,b). There are also other requirements, like: abTest(2,2)
should return a number && abTest(2,2)
should return 8
My code would cause me to pass the portion that says abTest(-2,2)
or (2,-2) should return undefined yet it would not pass the rest of the requirements.
Here is an example of what I was entering which would pass the 2 “undefined” requirements but would not pass the rest:
// Setup
function abTest(a, b) {
// Only change code below this line
if (a || 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);
All I changed to get all requirements to pass was entering the if statement as this instead:
if (a < 0 || b < 0) {
return undefined;
}
My assumption is that with (a || b < 0) is that it’s reading it as “if a exists, or if b is less than 0…”