Code is Fixed with If statement; wanted help using switch statement..any one?

Hello guys!

I have solved this problem using if statement. However I just had a thought if we can also do it using… Switch statement ?

// 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(0,0);
}

I tried and 4 cases are working…2 cases are not working. Can any one help me do this ?

Switch statement:

// Setup
function abTest(a, b) {
  // Only change code below this line
switch (a,b){
  case a,b<0:
  return undefined;
  break;
}
  // Only change code above this line
return Math.round(Math.pow(Math.sqrt(a) + Math.sqrt(b), 2));
abTest(0,0);
}

If I’m doing any wrong correct me please!! Thank you!

You can’t do a comparision like < or > with a switch. It always checks for strict equality ===. Also, the switch only accepts one argument, you can’t pass in two values a,b. A switch is used to compare ONE value against a number of possible cases:

switch (a){
  case 0: return 'zero'; break;
  case 1: return 'one'; break;
  case 2: return 'two'; break;
}

This checks whether

a === 0 // or
a === 1 // or
a === 2
1 Like

jsdisco, Thanks mate

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