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.