Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 15 additions & 5 deletions calculator.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,19 @@
export function applyDiscountCents(subtotalCents, discountPercent) {
if (!Number.isInteger(subtotalCents) || subtotalCents < 0) {
throw new Error('subtotalCents must be a non-negative integer');
if (!Number.isFinite(subtotalCents) || subtotalCents < 0) {
throw new Error('subtotalCents must be a non-negative number');
}
if (!Number.isFinite(discountPercent) || discountPercent < 0 || discountPercent > 100) {
throw new Error('discountPercent must be between 0 and 100');
if (!Number.isFinite(discountPercent)) {
throw new Error('discountPercent must be numeric');
}
return Math.round(subtotalCents * (1 - discountPercent / 100));

const discounted = subtotalCents * (1 - discountPercent / 100);
return Math.round(discounted);
}

export function refundAmountCents(orderTotalCents, requestedRefundCents) {
if (!Number.isFinite(orderTotalCents) || !Number.isFinite(requestedRefundCents)) {
throw new Error('refund inputs must be numeric');
}

return Math.min(orderTotalCents, requestedRefundCents);
}
4 changes: 2 additions & 2 deletions test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import assert from 'node:assert/strict';
import { applyDiscountCents } from './calculator.js';
import { applyDiscountCents, refundAmountCents } from './calculator.js';

assert.equal(applyDiscountCents(10000, 25), 7500);
assert.throws(() => applyDiscountCents(-1, 10));
assert.throws(() => applyDiscountCents(1000, 150));
assert.equal(refundAmountCents(5000, 7500), 5000);
console.log('ok');