diff --git a/src/shipping.js b/src/shipping.js new file mode 100644 index 0000000..cdea25a --- /dev/null +++ b/src/shipping.js @@ -0,0 +1,11 @@ +export const FREE_SHIPPING_THRESHOLD = 5000; +export const BASE_RATE = 499; +export const PER_KG_RATE = 150; + +export function shippingCost(subtotalCents, weightGrams) { + if (subtotalCents > FREE_SHIPPING_THRESHOLD) { + return 0; + } + const kilograms = Math.floor(weightGrams / 1000); + return BASE_RATE + kilograms * PER_KG_RATE; +} diff --git a/src/shipping.test.js b/src/shipping.test.js new file mode 100644 index 0000000..2b87e79 --- /dev/null +++ b/src/shipping.test.js @@ -0,0 +1,11 @@ +import { test } from "node:test"; +import assert from "node:assert/strict"; +import { shippingCost } from "./shipping.js"; + +test("orders over the threshold ship free", () => { + assert.equal(shippingCost(6000, 2500), 0); +}); + +test("small orders pay the base rate plus weight", () => { + assert.equal(shippingCost(1000, 2000), 799); +});