Diego Correa
Diego Correa

Reputation: 11

switch statement always goes to default in javascript

I want to do a simple program that prints out different phrases depending on whether a specific condition is met. We input 2 integer parameters in a function and it evaluates whether one number is greater, less than, or whether both numbers are equal to each other. My code looks like this:

function CheckNums(num1,num2) {
switch (num1,num2) {
  case (num2>num1):
   console.log('num2 greater than num1');
   break;
  case (num2 === num1):
   console.log('both equal');
   break;
  default:
   console.log('num1 greater than num2');
  }
}

Either case, whether I input: CheckNums(4,5) , CheckNums(2,2) , CheckNums(5,4) the program runs default.

IMPORTANT: Im not asking for an alternative solution to this problem. I already managed to solve it using if statements or the ternary operator. What I'm asking is: WHY IS THIS SWITCH NOT WORKING?

Questions that I`ve consulted that didn't solve my problem:

JavaScript switch statement only executes the default case

The sol:This guy had a problem where the Math.random() generator only generated numbers with decimal places, which of course, wouldn't go to either case except default.

JS Switch case not working correctly always default is executed

The sol:This guy was inputing strings as a parameter, and was expecting int values in the cases.

Other notes:

-Maybe the answer lies on using parseint? (Assuming that the parameters are not being inputed as ints.)

-I removed the parenthesis that go in the cases. That didn't solve my problem.

-For the second case, I also experimented using equality operator(==) instead of the Identity operator (===). The second case still did not work.

My best guess for where the problem is hiding: When I compiled the program in google chrome console, I get the following notification: "undefined". So, I think that's whats not allowing my cases to evaluate the parameters properly. Something is not being defined properly.

Upvotes: 1

Views: 4546

Answers (3)

Abhijeet_IXR
Abhijeet_IXR

Reputation: 947

An explanation as to why it does not work is provided here. This is to do with how the switch actually works.

Here is the modified code that works:

function CheckNums(num1, num2) {
  switch (true) {
    case num2 > num1:
      console.log("num2 greater than num1");
      break;
    case num2 === num1:
      console.log("both equal");
      break;
    default:
      console.log("num1 greater than num2");
  }
}

CheckNums(5, 6);

Upvotes: 0

Bergi
Bergi

Reputation: 664969

WHY IS THIS SWITCH NOT WORKING?

Because what you have written is equivalent to

const temp = num1,num2; // comma operator evaluates to the second operand
if (temp === (num2>num1)) {
   console.log('num2 greater than num1');
} else if (temp === (num2 === num1)) {
   console.log('both equal');
} else {
   console.log('num1 greater than num2');
}

You're comparing a number to booleans, which is never true, so given none of the cases match the default is executed.

Upvotes: 1

James
James

Reputation: 22247

If you really want to use a switch statement to do that you'll have to be clever. Hint: an IF is way easier.

function CheckNums(num1, num2) {
  switch (Math.sign(num1 - num2)) {
  case -1:
   console.log('num2 greater than num1');
   break;
  case 0:
   console.log('both equal');
   break;
  case 1:
   console.log('num1 greater than num2');
   break;
  default:
   console.log('Something went wrong');
   break;
  }
}

CheckNums(8, 5);
CheckNums(3, 4);
CheckNums(10, 10);
CheckNums("argh", "argh");

Upvotes: 2

Related Questions