diff --git a/src/cart.js b/src/cart.js index 0acba56..70a2850 100644 --- a/src/cart.js +++ b/src/cart.js @@ -8,6 +8,10 @@ export function applyDiscount(total, percent) { return Math.round(total - (total * percent) / 100) } +export function tax(cents, ratePercent) { + return Math.round((cents * ratePercent) / 100) +} + export function formatPrice(cents) { return `$${(cents / 100).toFixed(2)}` } diff --git a/test/cart.test.js b/test/cart.test.js index 5b4743b..c8a6ad5 100644 --- a/test/cart.test.js +++ b/test/cart.test.js @@ -1,6 +1,6 @@ import test from 'node:test' import assert from 'node:assert/strict' -import { subtotal, applyDiscount, formatPrice, isValidCoupon, shipping, total } from '../src/cart.js' +import { subtotal, applyDiscount, tax, formatPrice, isValidCoupon, shipping, total } from '../src/cart.js' test('subtotal multiplies price by quantity', () => { assert.equal(subtotal([{ price: 250, qty: 2 }, { price: 100, qty: 3 }]), 800) @@ -18,6 +18,15 @@ test('applyDiscount of zero percent changes nothing', () => { assert.equal(applyDiscount(1000, 0), 1000) }) +test('tax takes a percentage rate', () => { + assert.equal(tax(1000, 10), 100) + assert.equal(tax(999, 10), 100) +}) + +test('tax of zero rate is zero', () => { + assert.equal(tax(1000, 0), 0) +}) + test('formatPrice always shows two decimals', () => { assert.equal(formatPrice(1250), '$12.50') assert.equal(formatPrice(700), '$7.00')