Basic JavaScript - Return Early Pattern for Functions

Tell us what’s happening:
Describe your issue in detail here.

Your code so far

// Setup
function abTest(a, b) {
  // Only change code below this line
  if (a < 0 || b < 0) {
    return undefined; // Fail case: Return undefined if either a or b is negative
  }
  
  if (a === 0 && b === 0) {
    return 0; // Passed case: Return 0 if both a and b are 0
  }
  
  // Calculation based on the given input parameters
  return (a+b) * 2; 
}

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/114.0.0.0 Safari/537.36

Challenge: Basic JavaScript - Return Early Pattern for Functions

Link to the challenge:

Don’t change things you aren’t asked to change. The only thing you were asked to do is:

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

You did do that, but then you made other changes and the tests don’t like unexpected changes, so change everything else back to what is was.

Hello bbsmooth, I need assistance in writing the abTest function. I want to make sure that when I call abTest(2, 8), it returns 18. please guide me on how to write the function to achieve this? Thank you for your help!

As always - a bit tricky because it is so simple :wink: (Otherwise I couldn’t have solved it, could I?)

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

and

Remember that undefined is a keyword, not a string.

(Quotes from the challenge)

Let’s rephrase:

IF a OR b IS LESS THAN zero do nothing and immediately return.

Your job is very limited, you are only responsible for whether the function may be used. Or not.

Reading educates!

PS In preparation for the task I looked up the syntax for “if”, didn’t I?

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