Return Early Pattern for Functions Problem [SOLVED]

Return Early Pattern for Functions Problem [SOLVED]
0

#1

I’m not sure if I’m doing this right at all as I’m a little confused by it but I somehow got everything ticked correct except one part which is the -2,2 not equaling undefined. Anyways what do you think the problem is?

// Setup
function abTest(a, b) {
  // Only change code below this line
  if (a && b < 0) {
    return undefined;
    } 
  
  
  
  // 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);

#2

I’ve edited your post for readability. When you enter a code block into the forum, remember to precede it with a line of three backticks and follow it with a line of three backticks to make easier to read. See this post to find the backtick on your keyboard. The “preformatted text” tool in the editor (</>) will also add backticks around text.


#3

Mistake one would be using && as this would require both to be negative what you need is … || … (or )
this would then read … if a or b is less than 0
next little error is the way its written it should be if(a < 0 || b < 0)
use it this way and it will work


#4

Just to add to @JohnL3’s explanation.

You might want to also check out the operator precedence and associativity explanation on MDN, which explains that < has higher precedence than &&, so

(a && b < 0)

is first evaluated as (b < 0), which is false as b == 2.

Next (a && false) is evaluated, false is obviously false, but note that any number apart from 0 will be truthy see MDN Truthy glossary. So a alone is regarded as true, so this becomes true && false, which returns false, the result of (a && b < 0) for a ==-2 and b ==2.

Try playing around with these expressions in the browser dev tools console if you’re unsure of how it works.