Return Early Pattern for Functions

The problem was “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 solved it this way:
// Setup

function abTest(a, b) {

// Only change code below this line

switch (a, b){

case a < 0:

return undefined;


case b < 0:

return undefined;



// Only change code above this line

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



my solution wasn’t accepted can someone please explain why

Can you post a link to the challenge?

You can’t really switch on two variables like that. I mean, you can, but it will only base the switch on the last variable in the list. Personally, I would not use a switch here, because you have two variables you need to check, which a switch isn’t really going to help you with.

Do you think you can figure out another way to say “If a is less than 0 or b is less than 0 then return undefined” in code?

1 Like

Basic JavaScript: Return Early Pattern for Functions |

Thank you very much for this explanation. I guess I was really just excited cause I had just learnt switch and was eager to try it out hehe… I’d stick to using if (a < 0 || b < 0). Once again thanks very much I appreciate.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.