From 363d3e23a7abf6ccb1a10f0ae524d2e1b6d9e20c Mon Sep 17 00:00:00 2001 From: zero-logic0316 Date: Mon, 29 Jun 2026 21:06:14 +0800 Subject: [PATCH] fix: resolve 5 bugs in business logic - Fix discount calculation to apply percentage properly (fixes #25) - Fix cart total to include item quantities (fixes #26) - Fix username normalization to collapse all whitespace (fixes #27) - Fix shipping estimate to preserve cents (fixes #28) - Fix email validation to require domain suffix (fixes #29) --- src/business.js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/business.js b/src/business.js index a8f0cd0..ecb0a8b 100644 --- a/src/business.js +++ b/src/business.js @@ -8,17 +8,17 @@ function calculateDiscountedPrice(price, discountPercent) { } // BUG: treats a percentage discount as a flat currency amount. - return roundToCents(price - discountPercent); + return roundToCents(price - (price * discountPercent / 100)); } function totalCart(items) { // BUG: ignores quantity, so multi-quantity line items are undercounted. - return roundToCents(items.reduce((total, item) => total + item.price, 0)); + return roundToCents(items.reduce((total, item) => total + item.price * (item.quantity || 1), 0)); } function normalizeUsername(username) { // BUG: replaces only the first space and does not collapse repeated whitespace. - return username.trim().toLowerCase().replace(' ', '-'); + return username.trim().toLowerCase().replace(/\s+/g, "-") } function estimateShipping(weightKg, expedited = false) { @@ -32,12 +32,12 @@ function estimateShipping(weightKg, expedited = false) { } // BUG: rounds down and loses cents. - return Math.floor(estimate); + return roundToCents(estimate); } function isValidEmail(email) { // BUG: accepts values like `person@example` because it only checks for `@`. - return /^\S+@\S+$/.test(email); + return /^\S+@\S+\.\S+$/.test(email); } module.exports = {