How to calculate the largest remainder with Javascript
Tags: javascript
Published: 2023-08-28 12:07:16

With a focus on innovating and continuing to scale our product catalogues, Woovi decided in the first half of 2023 to bring solutions that does not exist for Pix yet.

With this, we can provide different ways for our merchants to sell beyond the pix in sight.

With that in mind, we launched [Pix Crediário](https://woovi.com/en/pix/pix-crediario/) and [Woovi Parcelado](https://woovi.com/en/pix/pix-parcelado/). Two installments products for Pix Instant Payment in Brazil. 

But what do they have in common?

Both work with installments and thinking in even numbers that's ok. But when we get to odd numbers, the world of installments can be a bit tricky.

But that already has a solution. We will teach you how we use the Largest Remainder Method algorithm to calculate our installments correctly without any surprise for the final customer.

## Largest Remainder Method
The "Largest Remainder" method is a technique used to distribute amounts proportionally, as in the case of installments. 

Let's imagine we want to calculate an order, bill, sell of 100$ in 3 installments. Only dividing by 3 is not enough. We can't set three installments of 33,33$.

Then, we need to calculate the bigger value between then and set as our first installment.

To calculate this in js you can follow the code below:

```ts
const largestRemainder = (amount: number, numParts: number) => {
  const individualAmount = Math.floor(amount / numParts);
  const remainders = [];
  let totalRemainder = 0;

  // Calculate the remainders and total remainder
  for (let i = 0; i < numParts; i++) {
    const remainder = amount - (individualAmount * numParts) + individualAmount;
    remainders.push(remainder);
    totalRemainder += remainder;
  }

  // Distribute the total remainder proportionally among parts
  for (let i = 0; i < numParts; i++) {
    const proportion = remainders[i] / totalRemainder;
    remainders[i] = Math.floor(proportion * totalRemainder);
  }

  return remainders;
}

const totalAmount = 100; // Total amount to be divided
const numParts = 3;      // Number of parts/parcels

const parcelAmounts = largestRemainder(totalAmount, numParts);
console.log(parcelAmounts); // An array containing the calculated parcel amounts
```

In this example, the largestRemainder function calculates the installments based on the total amount (100$) and the number of installments (3). It distributes the difference between the sum of the individual shares and the total value proportionally among the shares to ensure that the sum of the shares equals the total value. The result will be an array with the values of the calculated plots.

Keep in mind that this is a rough approximation and in some cases there may be small rounding differences. For more complex scenarios, consider using more advanced math libraries or more accurate calculation methods.

---

If you want to work in a startup in its early stages, This  is your chance. [Apply today!](https://woovi.com/jobs/)

---

Visit us [Woovi](https://woovi.com/)!

---

Foto de <a href="https://unsplash.com/pt-br/@mediamodifier?utm_source=unsplash&utm_medium=referral&utm_content=creditCopyText">Mediamodifier</a> na <a href="https://unsplash.com/pt-br/fotografias/I3HPUolh5hA?utm_source=unsplash&utm_medium=referral&utm_content=creditCopyText">Unsplash</a>