0
How can I check which number is greater,lesser using two variables in JavaScript?
I have to do it using switch/case. It is for a course I am taking. var NumOne = 10, NumTwo = 13; switch(NumOne,NumTwo) { case(NumTwo < NumOne): alert(numTwo + " is not less than " + NumOne); break; case(NumTwo > NumOne): alert(numTwo + " is greater than " + NumOne); break; } This is what I have come up with but I would like to have a working example... Can anyone help?
6 Respuestas
+ 23
alert((NumOne>NumTwo?NumOne:NumTwo)+" is the biggest.");
+ 12
switch (NumOne > NumTwo) {
case true: //NumOne is greater;
break;
case false: //NumTwo is greater;
break;
default: //equal;
}
?
+ 2
Thanks for the replies :) I also got it running using:
var var1 = 10,
var2 = 13;
var x = var1 > var2,
y = var1 < var2;
switch (x, y) {
case x:
alert(var1 + " is bigger than " + var2);
break;
case y:
alert(var1 + " is less than " + var2);
break;
}
+ 1
What about if statement? I think its easier and better way of doing that or first guy's answer to your question
+ 1
o
0
Yes with "if" and with ValentinHacker's code it will be easier, but my assignment was to do it with switch/case