I want to use a variable in a switch case. as an example, could not do it. Can you help?
switch(basket,pay){
case true, true:
blah blah...
break;
case false,true:
blah blah..
break;
case false, false:
blah blah...
break;
}
What you're looking for is flags and bitmasks. Basically you store myltiple pseudo-boolean values insdie the same variable and you can access their compound condition. The beauty of this is that you can compound multiple properties into a single variable. This is especially neat when there are a lot more boolean properties which you need to keep track of
var FLAG_BASKET = 0x1; // 0001
var FLAG_PAY = 0x2; // 0010
//set them both to pseudo-true
var flags = FLAG_BASKET | FLAG_PAY;
switch (flags){
case 0x0://both are false
case 0x1://basket is true, pay is false
case 0x2://basket is false, pay is true
case 0x3://both are true
}
Fiddled
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With