Search code examples
javascriptrpgmakermv

RPG Maker MV: Formula Correction (ternary not working)


Using a program called RPG Maker MV, within the program it allows for a ternary operator.

My question is how do I format this to create accurate calculations:

a.atk * 5 / b.def * 4 >= 1 ? a.atk * 5 / b.def * 4 : 1

I would like the equation to take: (a * 5) then divide by (b * 4), while being greater than 1

If true: Then use that formula, else/otherwise use 1.

Thus; if a number is lower than 1 or negative, it will simple 'convert' it to 1 (one).

Within the program (RPG Maker MV), these numbers are set else-where in the program, but are set for sure prior to asking the question.

a.atk represents the variable user's attack. (A can be the value of 2 if that helps).

b.def represents the variable enemy defense. (B can be the value of 3 if that helps).


Solution

  • Use parentheses to make the multiplications occur before the divisions. Since those operators have the same precedence, they'd get executed left to right, so in your case the division would go before the last multiplication and mess up the result.

    var a = 2;
    var b = 2;
    var result = (a * 5) / (b * 4) >= 1 ? (a * 5) / (b * 4) : 1
    //1.25