Calculate a percentage discount in JavaScript
Beginner · 3 min read
"20% off" signs, voucher codes and member prices all come down to the same calculation: take a percentage of a price and subtract it. It is simple until a shop runs two discounts at once. Is "10% off, then another 10% off" the same as 20% off? Many people guess yes, and so does a lot of code.
In this tutorial you will write discount and afterDiscount, use them to take 20% off a price, then stack two 10% discounts and find out how much is really taken off. Prices are in kobo (100 kobo make one naira) so every amount is a whole number. You need to know what a function is and what Math.round does.
Percentages in plain words
"Percent" means "out of a hundred". 20% of a price is the price times 20, divided by 100:
console.log((1000000 * 20) / 100); // 200000
1,000,000 kobo is ₦10,000, and 20% of it is 200,000 kobo, ₦2,000. Not every price divides neatly, so the discount needs rounding to a whole kobo.
Discounts on top of discounts
When a second discount is applied, it is applied to the price that is left after the first one, not to the original price. Keep that in mind: it is the whole surprise in this exercise.
Here is the exercise. Try it yourself first if you like, or read on and we will build it together.
65 A discount, and two discounts stacked
easyTake 20% off a price in kobo. Then apply 10% and another 10% and check whether that equals 20% off.
Stuck? Three hints, smallest first
- 1. The discount is
Math.round(kobo * percent / 100); subtract it. - 2. Stacked discounts multiply, so each one applies to what is left after the previous.
- 3. Two 10% discounts leave 0.9 × 0.9 of the price. Work out what that is as a single percentage.
Step 1: the size of the discount
Work out the discount on its own and round it:
const discount = (kobo, percent) => Math.round((kobo * percent) / 100);
console.log(discount(1000000, 20)); // 200000
console.log(discount(999, 10)); // 100
10% of 999 kobo is 99.9 kobo, which rounds to 100.
Step 2: the price after the discount
Subtract the rounded discount from the price:
const discount = (kobo, percent) => Math.round((kobo * percent) / 100);
const afterDiscount = (kobo, percent) => kobo - discount(kobo, percent);
console.log(afterDiscount(1000000, 20)); // 800000
console.log(afterDiscount(1000000, 0)); // 1000000
console.log(afterDiscount(1000000, 100)); // 0
20% off ₦10,000 is ₦8,000. The two extreme cases make sense too: 0% changes nothing and 100% makes it free.
Notice the order: round the discount first, then subtract. That way the discount and the price you pay always add back to the original price exactly:
const discount = (kobo, percent) => Math.round((kobo * percent) / 100);
const afterDiscount = (kobo, percent) => kobo - discount(kobo, percent);
console.log(afterDiscount(999, 10)); // 899
console.log(discount(999, 10) + afterDiscount(999, 10)); // 999
If you rounded both halves separately, they could add up to one kobo more or less than the price, and a receipt would not balance.
Step 3: stack two 10% discounts
Feed the result of one discount into the next:
const discount = (kobo, percent) => Math.round((kobo * percent) / 100);
const afterDiscount = (kobo, percent) => kobo - discount(kobo, percent);
const price = 1_000_000;
const twenty = afterDiscount(price, 20);
const tenTwice = afterDiscount(afterDiscount(price, 10), 10);
console.log(twenty); // 800000
console.log(tenTwice); // 810000
console.log(tenTwice - twenty); // 10000
The underscore in 1_000_000 is only there to make the number easy to read; JavaScript ignores it.
Two 10% discounts leave 810,000 kobo, ₦100 more than one 20% discount. The first 10% takes ₦1,000 off ₦10,000. The second 10% is taken from the ₦9,000 that is left, so it only takes ₦900.
Step 4: what percentage is that really?
Each 10% discount leaves 0.9 of the price. Two of them leave 0.9 times 0.9, which is 0.81 of the price. So the total taken off is 19%, not 20%:
const discount = (kobo, percent) => Math.round((kobo * percent) / 100);
const afterDiscount = (kobo, percent) => kobo - discount(kobo, percent);
const price = 1_000_000;
const tenTwice = afterDiscount(afterDiscount(price, 10), 10);
console.log(((price - tenTwice) / price) * 100); // 19
console.log(afterDiscount(afterDiscount(price, 10), 20)); // 720000
console.log(afterDiscount(afterDiscount(price, 20), 10)); // 720000
The last two lines show that the order of two discounts doesn't matter here: 10% then 20% gives the same price as 20% then 10%. What matters is that they multiply, not add.
The full solution
The answer panel has discount and afterDiscount, prints 20% off ₦10,000, the stacked 10% discounts, the gap between them, and both orders of a 10% and a 20% discount. Its checks cover the 19% result, a full and a zero discount, and the kobo that rounding keeps in place. Press Run to see the output.
const discount = (kobo, percent) => Math.round((kobo * percent) / 100);
const afterDiscount = (kobo, percent) => kobo - discount(kobo, percent);
const price = 1_000_000; // 10,000 naira
const twenty = afterDiscount(price, 20);
const tenTwice = afterDiscount(afterDiscount(price, 10), 10);
console.log('20% off ->', twenty, 'kobo');
console.log('10% then 10% ->', tenTwice, 'kobo <- not the same');
console.log('difference ->', tenTwice - twenty, 'kobo');
console.log('order does not matter:', afterDiscount(afterDiscount(price, 10), 20), afterDiscount(afterDiscount(price, 20), 10));
Output
8 checks that prove this answer
You do not have to read these. They are part of the answer panel above, so pressing
Run there executes all 8 of them and prints ok for each.
That output is the proof, and the build fails if a single check fails.
test('20% off ten thousand naira', twenty, 800000);
test('two 10% discounts leave more', tenTwice, 810000);
test('the stack is really 19%, not 20%', ((price - tenTwice) / price) * 100, 19);
test('the order of two discounts does not matter', afterDiscount(afterDiscount(price, 10), 20), afterDiscount(afterDiscount(price, 20), 10));
test('a full discount is free', afterDiscount(price, 100), 0);
test('no discount changes nothing', afterDiscount(price, 0), price);
test('rounding happens on the discount, so a stray kobo stays put', afterDiscount(999, 10), 899);
test('and the parts still add back to the whole', discount(999, 10) + afterDiscount(999, 10), 999);
"10% off, plus another 10%" is 19% off, not 20%, because the second discount applies to a smaller number. Retail copy exploits this and so do bugs: applying a voucher and a sale price in two separate steps gives a different total from applying their sum, and whichever the customer sees first is the one they will argue for.
The last assertion is the invariant that keeps a receipt honest — the discount and the remainder must add back to the original. Round the discount, then subtract, rather than rounding both halves independently.
Common mistakes
- Adding stacked percentages. 10% plus 10% is 19% off, because the second one applies to a smaller price.
- Rounding the discount and the new price separately. They can then add up to a kobo more or less than the original. Round the discount, then subtract.
- Forgetting to divide by 100.
kobo * percentalone is twenty times the price, not 20% of it.
Try it yourself
- Work out the real total discount of two 50% discounts in a row. (It is not 100%.)
- Write
stack(kobo, percents)that applies an array of percentages one after another, usingreduce. - A shop wants one discount equal to "20% then 10%". Find the single percentage that gives the same price.