Basic JavaScript - Return Early Pattern for Functions

Tell us what’s happening:
I passed the test but I don’t understand how abTest (2, 8) should return 18, nor do I understand how abTest(3, 3) should return 12.

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));
}

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/113.0.0.0 Safari/537.36 Edg/113.0.1774.57

Challenge: Basic JavaScript - Return Early Pattern for Functions

Link to the challenge:

Are you sure your current code shown here passes? There is actually an issue with the code, and doesnt pass

To explain the code at the bottom

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

First, it takes the square root of a and b and adds them together. For this example, if a and b were both three then its

1.7320508075688772 + 1.7320508075688772

which equals

3.4641016151377544

After the square root is added together then it uses the new value of the square roots and raises it by the power of two using Math.pow. So then it will be

3.4641016151377544^2

which equals

11.999999999999998704045688274719

Then finally it uses Math.round to round up the number which is

12

I am aware of the lack of a closing curly bracket. I started writing the post before completing the actions and running the test. After the test I didn’t throw away what I wrote. Any way, thank you for the explanation. I understand now.

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