Basic JavaScript - Return Early Pattern for Functions

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

  • Failed:abTest(2, 2) should return a number

  • Failed:abTest(2, 2) should return 8

  • Passed:abTest(-2, 2) should return undefined

  • Passed:abTest(2, -2) should return undefined

  • Failed:abTest(2, 8) should return 18

  • Failed:abTest(3, 3) should return 12

  • Passed:abTest(0, 0) should return 0

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

Challenge: Basic JavaScript - Return Early Pattern for Functions

Link to the challenge:

So close.
However if you check the lesson again you will find this explanation:

if `a` or `b` are less than `0`

you conditional:

if (a > 0 || b > 0){

a > 0 → Does this meet the requirement?

no it does not meet the requirement but once I change the a > 10 it accepts all the others expect the undefined

So, for what I understand you conditional looks like this:

if (a > 10){

You said “once I change the a > 10 it accepts all the others except the undefined”

It half works yes, however you conditional is not following the requirements, imagine having 10.000 tests. Maybe the 50 % or less will be a success and the rest a fail

Let me explain:

The tests using your conditional

if (2 > 10):
    2 > 10 (2 is bigger than 10?)

    The condition is false (Expected Output: False).

if (-2 > 10):
    -2 > 10  (-2 is bigger than 10?)

    The condition is false (Expected Output: True).

if (2 > 10):
    2 > 10  (2 is bigger than 10?)

    The condition is false (Expected Output: True).

if (2 > 10):
    2 > 10  (2 is bigger than 10?)

    The condition is false (Expected Output: False).

if (3 > 10):
    3 > 10  (3 is bigger than 10?)

    The condition is false (Expected Output: False).

if (0 > 10):
    0 > 10  (0 is bigger than 10?)

    The condition is false (Expected Output: False).

Looking at the tests and using your conditional, we can see that, it will always return false, however the expected return is that it should return true on two tests at least.

Tasks on the lesson:

if `a` or `b` are less than `0` will return undefined

You can interpret this line so:

"A" can't be less than 0
"B" can't be less than 0
Returns undefined if so

Check the documentation for the less operator < and try it again

Cheers

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