Return Early Pattern for Functions -ra

Return Early Pattern for Functions -ra
0
#1

Tell us what’s happening:

i don’t get it how to fix

Your code so far


// Setup
function abTest(a, b) {
  // Only change code below this line
  switch(a, b < 0){
    case a: 
    case b:
    console.log("undefined")
    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);
Modify the function abTest so that if a or b are less than 0 the function will immediately exit with a value of undefined.
0 Likes

#2

Are you familiar with how to chain together expressions in javascript yet? There are a few things to remember:

  • To express if (this is true OR that is true) we would use if (this == true || that == true);
  • to express if (this is true AND that is true), we would use if(this==true && that==true)

So how would you show if a < zero or b < zero, do you think?

And be careful – are you being told to return a string that says “undefined”, or are you told to return undefined? They are two very very different things.

1 Like

#3

really I can’t understand, can you explain i little better what I can do here?

0 Likes

#4

The challenge is to return undefined if a or b are negative

A switch statement is not a good choice because it uses strict comparison, and can’t be used if you want to say that something must be less than something else to do something

What can you use then?

2 Likes

#5

maybe if/else if ?

0 Likes

#6

So the example I was giving yesterday may have been too “theoretical.” Let me make a more concrete example.

Suppose I wished to check whether my userName is snowmonkey and my age is over 40. That would be an AND condition, as both things must be true, so it would look something like:

if (userName == "snowmonkey" && userAge > 40){ ... }

And that’s great, but that’s not really what you need. You are looking for EITHER thing to fail. So that would be more like “if my userName is snowmonkey OR my userAge is over 40”, and that might look more like:

if(userName == "snowmonkey" || userAge > 40) {...}

In that one, if either one or both conditions are met, then we can go ahead. What are the two conditions you have to meet? Can you see a way this might apply?

1 Like

#7
Modify the function abTest so that if a or b are less than 0 the function will immediately exit with a value of undefined.
// 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);

// running tests

abTest(2,2) should return a number

abTest(2,2) should return 8

abTest(-2,2) should return undefined

abTest(2,-2) should return undefined

abTest(2,8) should return 18

abTest(3,3) should return 12

// tests completed

0 Likes

#8

So your code says:

if we HAVE an a, OR if b < 0

Is that what you actually mean to check for?

1 Like

#9
// 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);
// running tests
abTest(-2,2) should return undefined
abTest(2,-2) should return undefined
// tests completed
0 Likes

#10

You were closer with the last post. It is an or operation, not an and, but the thing is, you are thinking you can check “if a or b are less than zero”, but you can’t. Just can’t be done.

You must check “if a is less than zero OR b is less than zero”. Each side of that is a totally separate thing. There is no connection between them, and there is no shortcut.

So use the || instead of the &&. Now tell me:

  • How would you code "a is less than zero"?
  • How would you code "b is less than zero"?

When you have each piece, separately, then you can use the || to join them.

1 Like

#11
 if( a < 0 || b <0){
``` ?

abTest(-2,2) should return undefined
abTest(2,-2) should return undefined

0 Likes

#12

And that’s exactly right. But now, go back to the very first response I gave you in this thread, and read the last two lines. You’re returning “undefined” – is that what it asks you to return?

undefined is a reserved keyword in javascript, it actually represents something. This is taken from the Mozilla Developer docs (MDN) about the keyword undefined:

A method or statement also returns undefined if the variable that is being evaluated does not have an assigned value.

So you’re returning “undefined”, the string, while the test wants to see undefined, the reserved keyword. How would you get there?

0 Likes

#13

Actually thats where i am also stuck and i am required to return undefined and my codes so far

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

and my error

abTest(-2,2) should return undefined
abTest(2,-2) should return undefined
0 Likes

#14

@snowmonkey i got the answer from your explanation you are a Hero :v:

0 Likes