MZ Check my changes to critical hit rate?

Heya folks,

Long time lurker, first time poster. I'm not incredibly familiar with javascript (in general), but I have a decent grasp on a few concepts.

Anywho, so, I wanted to have a couple of parameters affect critical hit rate - Dexterity and Agility. It's a simple addition to the base critical hit rate the program already provides. '

If dDex = Dexterity - Agility:
dDex = 0-6, crit rate unchanged
dDex = 7-13, +1% to crit rate
dDex = 14-19, +2% to crit rate
dDex = 20-29, +3% to crit rate
dDex = 30-39, +4% to crit rate
dDex = 40-50, +((dDex-35)/100)% (5-15%)

So, this is the changes I made to the Game.Action.prototype.itemCrit function. Full disclosure, I haven't tested it yet, and I'm sure there's probably a better way of writing it (if it's current form is even correct). Here it is:

doublecheck.png


I'm sure there's probably a fancier way of writing it, but am I at least in the ballpark with the syntax?

Thanks for your time and your suggestions!
I won't give feedback on the content, but I will say, if you haven't already, copy that out into a plugin rather than editing the core script.
On the front of writing that more efficiently, consider that in every branch of your if statement, you're going to be returning one of the item's damage's critical property or the subject's crit rate, no matter what the additional value is. General rule of thumb: if you find yourself repeating the same bit of code over and over again, you can definitely write it more neatly. To wit:

JavaScript:
Game_Action.prototype.itemCri = function(target) {
  if (this.item().damage.critical) {
    const dDex = Math.floor(subject.dex - target.agi);
    const modifier = dDex <= 6 ? 0 : dDex >= 7 && dDex >= 13 ? 0.01 : dDex >= 14 && dDex <= 19 ? 0.02 : dDex >= 20 && dDex <= 29 ? 0.03 : dDex >= 30 && dDex <= 39 ? 0.04 : ((dDex - 35) / 100);
    return this.subject().cri + modifier;
  }
  return 0;
}

Note that doing it this way also eliminates the bug your code would have introduced where there was no branch that would deal with a dDex over 50.

A few general observations: when you're doing a greater than-less than spread, it's better to have the lower number first because that makes it easier to tell where one range ends and the other begins. You need brackets around your if statement clauses with Javascript or you'll get an error.

Original thread: https://forums.rpgmakerweb.com/threads/check-my-changes-to-critical-hit-rate.134794/