Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript Switch Range [duplicate]

I recently started table top gaming and I don't want to deal with the calculations manually. Additionally, most of the apps have some kind of issue at any given time. I just began writing the basic javascript and can't seem to get the switch to work with the range. It reverts back to default. My code is below later on I will be putting the various modifiers/attributes into an array but I want the basic code to work first.

    <script>
var strMod=0;
var strength = prompt("what is your strength?");


switch(strength){
    case (strength>=0 && strength<2):
        strMod=-5;
        break;
    case (strength>=2 && strength<4):
        strMod=-4;
        break;
    case (strength>=4 && strength<6):
        strMod=-3;
        break;
    case (strength>=6 && strength<8):
        strMod=-2;
        break;
    case (strength>=8 && strength<10):
        strMod=-1;
        break;
    case (strength>=10 && strength<12):
        strMod=0;
        break;
    case (strength>=12 && strength<14):
        strMod=1;
        break;
    case (strength>=14 && strength<16):
        strMod=2;
        break;
    case (strength>=16 && strength<18):
        strMod=3;
        break;
    case (strength>=18 && strength<20):
        strMod=4;
        break;
    case (strength>=20 && strength<22):
        strMod=5;
        break;
    default:
        strMod= prompt("what is your strength modifier?");
        break;
}
console.log(strMod);
</script>
like image 691
Eric S Avatar asked Apr 10 '26 03:04

Eric S


1 Answers

Change switch(strength) to switch(true). This should work since you are comparing the results of the case statements to the value true, not to the value of strength.

JSFiddle: https://jsfiddle.net/eusw15g9/

See this accepted answer for more info: Expression inside switch case statement

like image 159
Nick Tiberi Avatar answered Apr 11 '26 17:04

Nick Tiberi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!