MZ Can a coder take a look at this code please?

So a while ago I got someone on here to write a little code for me. They made it so that Luck increases crit, hit, eva, and mev a bit. Here is their original code:

// Determine the variables used in this calculation.
let xparamId = arguments[0];
let base = this.traitsSum(Game_BattlerBase.TRAIT_XPARAM, xparamId);
let plus = this.xparamPlus(xparamId);
let paramRate = this.xparamRate(xparamId);
let flatBonus = this.xparamFlatBonus(xparamId);
let luckBonus = 0;

if (xparamId === 0 || xparamId === 1 || xparamId === 2) luckBonus = this.param(7) * 0.001;

if (xparamId === 4) luckBonus = this.param(7) * 0.0005;



// Formula to determine total parameter value.

let value = (base + plus) * paramRate + flatBonus + luckBonus;



// Final value

return value;


That worked. But then I tried to add some stuff to it and several stats showed up as "NaN", I will show what I tried to add:

// Determine the variables used in this calculation.
let xparamId = arguments[0];
let base = this.traitsSum(Game_BattlerBase.TRAIT_XPARAM, xparamId);
let plus = this.xparamPlus(xparamId);
let paramRate = this.xparamRate(xparamId);
let flatBonus = this.xparamFlatBonus(xparamId);
let luckBonus = 0;
let focusBonus = 0;

if (xparamId === 2) focusBonus = this.xparam(0) * 0.0005;
if (xparamId === 3) focusBonus = this.xparam(0) * 0.0005;

if (xparamId === 0 || xparamId === 1 || xparamId === 2) luckBonus = this.param(7) * 0.001;
if (xparamId === 4) luckBonus = this.param(7) * 0.0005;

// Formula to determine total parameter value.
let value = (base + plus) * paramRate + flatBonus + luckBonus + focusBonus;

// Final value
return value;

I wanted to make it so that hit increased crit and cev. But that broke it. What did I do wrong?
Who wrote this code? There are a number of variables that are being defined as things that don't exist anywhere in this block of code and would be context dependent on where it's called from.
The code itself looks OK, though it would get invoked twice for Ex-params 2 and 3. Perhaps VisuStella have implemented some kind of check to prevent recursive calls, rather than just a usual try-catch?

Try adding this 1 line to the original, working code:
JavaScript:
let focusBonus = this.xparam(0);
If that also fails, it probably means you have to avoid any xparam calls in that code. This might work:
JavaScript:
// Determine the variables used in this calculation.
let xparamId = arguments[0];
let base = this.traitsSum(Game_BattlerBase.TRAIT_XPARAM, xparamId);
let plus = this.xparamPlus(xparamId);
let paramRate = this.xparamRate(xparamId);
let flatBonus = this.xparamFlatBonus(xparamId);
let luckBonus = 0;
let focusBonus = 0;

// Luck bonus.
switch (xparamId) {
  case 0:
  case 1:
  case 2:
    luckBonus = this.param(7) * 0.001;
    break;
  case 4:
    luckBonus = this.param(7) * 0.0005;
    break;
}

// Focus bonus.
switch (xparamId) {
  case 2:
  case 3:
    const xparam0 = this.xparamRate(0) * (
                      this.traitsSum(Game_BattlerBase.TRAIT_XPARAM, 0)
                    + this.xparamPlus(0)
                  )
                  + this.xparamFlatBonus(0)
                  + this.param(7) * 0.001;  // luck bonus for xparam 0
    focusBonus = xparam0 * 0.0005;
    break;
}

// Formula to determine total parameter value.
let value = (base + plus) * paramRate + flatBonus + luckBonus + focusBonus;

// Final value
return value;
(I've written this using switch instead of if (a || b || c), but that's just a style choice.)
It's part of the VisuStella code, a part the user can edit. The person from here added a few things to it, the luckBonus stuff.

Original thread: https://forums.rpgmakerweb.com/threads/can-a-coder-take-a-look-at-this-code-please.164355/