Please I need help I'm stucked here: Return Early Pattern for Functions

Please I really need your help in this.

Tell us what’s happening:

Your code so far


// Setup
function abTest(a, b) {
  // Only change code below this line
  console.log();
  
  
  // 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);

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36.

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-javascript/return-early-pattern-for-functions

What is your problem about?
To give you random hints, in this challenge you are required to write an if statement and return from the function in case you hit the ‘true path’^^

This is the problem

Did the hint from @Layer above help you in any way?

What is it that you don’t understand?

I didn’t get it right still…
On the left side of the second screenshot is what I need to get so as to pass that particular exercise.
I’m confused a little about it… that’s why I sent the screenshot so you get see what I meant

The link to the challenge was included in your first post, the screenshot wasn’t necessary

Have you tried anything?

What is the code you have tried and didn’t pass?

We don’t know how to help you if you don’t show what you have tried

This is the code I have tried:

function abTest(a, b) {
// Only change code below this line
if (a, b = 2,2) {
return 8;
} else if (a, b = -2,2) {
return undefined;
} else if (a, b = 2,-2) {
return undefined;
} else if (a, b = 2,8) {
return 18;
} else if (a, b = 3,3) {
return 12;
}

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

First, you don’t write comparisons like that, = is the assignment operator

You shouldn’t hardcode values, you should create a function that takes any two values and return undefined or the right number

The part of the function that compute the result is already there, but it can work only if both numbers are positive

So you need to make sure that your function stop early if at least one of the numbers is negative

So, you need a few things

  • know how to check if a number is negative
  • know how to combine the check of two different numbers
  • know how to have a piece of code execute only under certain conditions
  • know how to return undefined
  • know how to combine all the previous things to have your code have the required behaviour

So, what is it of these things that you are missing?

1 Like