Simulating Debt Deduction Logic with JavaScript
How the Algorithm Your Network Provider Uses Might Work When Deducting Your Unpaid Balance
Recently, I owed my network provider a small amount of money.
Instead of paying back the debt directly, I tried to be sneaky and bought a new data bundle using my bank app without clearing the debt first.
To my surprise, the transaction went through. I got an alert confirming my data purchase. I even had this little grin on my face because I thought I had found some sort of loophole.
But when I checked my actual data balance…
Yeah, they didn’t give me the full bundle. I still got data, just not the full 1GB I paid for.
As an aspiring backend engineer, this got me curious. What kind of logic or algorithm are they using to deduct the debt before allocating the remaining data?
Even though this is a basic thing, I was still curious. While in the shower (classic), I started doing some quick mental math to simulate what might be happening.
The Math I Came Up With
Assumptions:
- You owe ₦200.
- You buy a data bundle worth ₦500 (1GB of data).
We know:
- 1GB = 1,000,000KB
To figure out how much data (in KB) you get per ₦1:
₦500 = 1,000,000KB
₦1 = 1,000,000 / 500 = 2,000KB
Now to remove your debt:
Debt = ₦200
Data to deduct = 200 × 2,000KB = 400,000KB
So your final data balance would be:
1,000,000KB - 400,000KB = 600,000KB
600,000KB = 0.6GB
If This Were Code…
Let’s say you wanted to simulate this logic in JavaScript:
function removeDebt(debt, purchasedPrice, purchasedData) {
// Calculating price per naira
const kbPerNaira = purchasedData / purchasedPrice;
// Removing debt
const dataToDeduct = debt * kbPerNaira;
const finalData = purchasedData - dataToDeduct;
// Displaying balance
console.log(`Your Data Balance is: ${finalData}KB`);
// approximating to gb rounding to 2 dp
console.log(`Which is approximately: ${(finalData / 1_000_000).toFixed(2)}GB`);
}
// Example usage
removeDebt(200, 500, 1_000_000); // You bought 1GB for ₦500 while owing ₦200
the end
I don’t know if this was blog worthy but I had finished writing before I could consider so have a wonderful day my friend