help---Return Early Pattern for Functions

Hi there, can someone explain to me what I’m missing?

Here’s my code so far for this challenge:


// Setup
function abTest(a, b) {
  // Only change code below this line
  if (a < 0 || b < 0);
  return;
  if (a > 0 && b > 0);
  return (a*b) + a;
  if   (a == b);
  return (a*2*b);

  // 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 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.110 Safari/537.36.

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

If statements look like this:

if (condition) {
  doSomething();
}

The challenge asks you to return if a or b is less than 0. Otherwise just do the calculation that is already present.The challenge does not ask you to do any of these things, not sure why you added them:

if (a > 0 && b > 0);
return (a*b) + a;
if   (a == b);
return (a*2*b);

Yes, thank you for responding and taking a look. Originally, I did have it formatted as the following below:

if (a < 0 || b < 0);
return;

However, I seemed to still be missing something. Am I on the right track? I’m unsure what exactly is the missing piece.

ahh nevermind. It looks like where I had the colon after the “if” statement I should have had a pair of curly brackets instead.

Hey Guys, i was trying to do this challenge, but i don’t get how they got 8 on abTest(2,2)
Math.pow(Math.sqrt(a) + Math.sqrt(b), 2). according to my knowledge back to C, that expression is equal to squareroot of a+ squareroot of b power 2(didn’t get math sign)
can anyone tell me where am wrong

Math.sqrt(2) + Math.sqrt(2)
This is 2 * Math.sqrt(2)

This is now squared, so you get 4 * 2 which is 8

This is because the syntax of Math.pow() is Math.pow(base, exponent)
base ^ exponent

If your look Math.sqrt(a) + Math.sqrt(b) is the first argument of Math.pow()

1 Like

Ooh now get it. were trying to eliminate squareroot inside the equation. thanks buddy
what about the return?? still getting some trouble with return 0, return 1 and return only.

Do you mean the return on the line of that mathematical expression?
It returns the result of Math.pow rounded to the nearest integer (Math.round is the one that makes the rounding - it is needed because of limitations of doing this operations in binary with a limited number of bits available)

Or where?

yep agree, but what if we type return 1 instead of return??
BTW what’s the difference btn these two

I don’t understand, do you mean difference between

return; // never write this 

And

return 1; // this return 1

?

1 Like

Yes, cause i get trouble every time i use “return” stuff

Never write just return;, make it always return something

for this challenge we did it cause we want it to be undefined. am i right??

In that case you need to write return undefined

1 Like

thanx buddy for the explaination