Home > Mobile >  Recursive Function Uncaught RangeError: Maximum call stack size exceeded
Recursive Function Uncaught RangeError: Maximum call stack size exceeded

Time:02-10

This function which will execute when you enter on input field then it will calculate how much money do you have to return with x type to customer.

But getting stack size error sometime for 6 digit and everytime for 7 digit.

Reproduce: put 1234567 in input box and check the console.

Function:

let returnList = [];
let predictorList = [
    100, 50, 20, 10, 5, 1, 0.5, 0.25, 0.1, 0.05, 0.01,
  ];
let total = 11.23

function isRemainingMoney(value) {
    let remainingValue = value;

    // when remaning value becomes zero then return the returnlist
    if (remainingValue == 0) {
      return returnList;
    }

    for (let pRed of predictorList) {
      /* remainingValue is greater than predictor value then push it 
      eg:  41.33 > 20
           21.33 > 20
           1.33 > 1
           0.33 > 0.25
           0.08 > 0.05
           0.03 > 0.01 * 3 times
      */
      if (remainingValue >= pRed) {
        const isPredExist = returnList.find(
          (pItem) =>  pItem.money ==  pRed
        );
        if (!!isPredExist) {
          isPredExist.count  = 1;
          isPredExist.total = isPredExist.total    pRed;
        } else {
          returnList.push({
            type: pRed,
            money: pRed,
            count: 1,
            total: pRed,
          });
        }
        remainingValue =  remainingValue.toFixed(2) - pRed;
        break;
      }
    }
    // recursive call the same method untill remainivalue becomes zero.
    return isRemainingMoney( remainingValue.toFixed(2));
  }

document.querySelector('input').addEventListener('change', (event) => {
   if(!!event.target.value) {
        returnList.length = 0;
        returnList = isRemainingMoney( event.target.value - total);
        console.log(returnList, 'returnList');
   }
   
})

Playground: Current Output From real application:

CodePudding user response:

You end up having way too many recursive calls when the input value is large. It should be fairly straight forward to convert from recursive to iterative using a while loop. The only issue I ran into was floating point not getting down to 0 (like mentioned in the comment by @Keith). When doing money calculations, it is usually best to use integers. Use the smallest denomination in the currency. For example, in US currency, that would be cents. Only convert to decimal (or dollars in this case) when displaying the values.

I have also simplified your calculations a bit. Because of these changes, you could actually use recursion now, if you want since the maximum level of recursion now is predictorList.length.

let predictorList = [
  10000, 5000, 2000, 1000, 500, 100, 50, 25, 10, 5, 1
];
let total = 709;

function isRemainingMoney(value) {
  let returnList = [];

  // when remaning value becomes zero then return the returnlist
  while (value != 0) {
    for (let pRed of predictorList) {
      if (value >= pRed) {
        returnList.push({
            money: pRed / 100,
            count: Math.floor(value / pRed),
          });
        value %= pRed;
      }
    }
  }
  return returnList;
}
document.querySelector('input').addEventListener('change', (event) => {
  if (!!event.target.value) {
    let returnList = isRemainingMoney( event.target.value * 100 - total);
    console.log(returnList, 'returnList');
  }

})
<input type="number">

  • Related