From: bochard Date: Sat, 1 Mar 2025 11:24:36 +0000 (+0800) Subject: added an unfinished function, I think that's enough for today. X-Git-Url: https://git.bochard.net/?a=commitdiff_plain;h=43ac59baeb05f7037e6bc56692d400bf8740052e;p=simple-cash-register.git added an unfinished function, I think that's enough for today. --- diff --git a/index.html b/index.html index f7bc147..124690b 100644 --- a/index.html +++ b/index.html @@ -16,7 +16,7 @@
Total: $0.00
-
+
Change: $0.00
diff --git a/index.js b/index.js index c80ee77..eba34c7 100644 --- a/index.js +++ b/index.js @@ -6,32 +6,70 @@ const productList = document.getElementById('product-list'); const productQuantity = document.querySelectorAll('.product-quantity'); const changeDrawer = document.getElementById('change-drawer'); -let price = 1.87; +let totalChangeToReturn = 0; +let price = 19.5; // 1.87 - original let cart = {}; let totalPriceOnCart = 0; let cid = [ - ['PENNY', 1.01], - ['NICKEL', 2.05], - ['DIME', 3.1], - ['QUARTER', 4.25], - ['ONE', 90], - ['FIVE', 55], - ['TEN', 20], - ['TWENTY', 60], - ['ONE HUNDRED', 100] + ['PENNY', 1.01], // 101 pennies + ['NICKEL', 2.05], // 41 nickels + ['DIME', 3.1], // 31 dimes + ['QUARTER', 4.25], // 17 quarters + ['ONE', 90], // 90 one-dollar bills + ['FIVE', 55], // 11 five-dollar bills + ['TEN', 20], // 2 ten-dollar bills + ['TWENTY', 60], // 3 twenty-dollar bills + ['ONE HUNDRED', 100] // 1 hundred-dollar bill ]; -let productsPrice = { +const currency = { + 'PENNY': 0.01, + 'NICKEL': 0.05, + 'DIME': 0.10, + 'QUARTER': 0.25, + 'ONE': 1, + 'FIVE': 5, + 'TEN': 10, + 'TWENTY': 20, + 'ONE HUNDRED': 100 +} +const productsPrice = { 'water bottle': 1, 'bread': 2.5, 'coffee': 1.5 } -// ===== CASH IS GREATER THAN THE TOTAL PRICE IN CART ===== +// ===== TODO: CALCULATE THE CHANGE ===== +function calculateChange(amount) { + let reversedCid = [...cid].reverse(); + let changeToReturn = []; + + while (amount < currencyValue) { + + } + + reversedCid.forEach((arr) => { + const cashName = arr[0]; + const cashValue = arr[1]; + + while (currency[cashName] < amount) { + changeToReturn += amount - cashValue; + } + }) + totalChangeToReturn = changeToReturn; +} + +// ===== CHECK IF CASH IS GREATER THAN THE TOTAL PRICE IN CART ===== function checkCashAmount(cash) { if (cash < totalPriceOnCart || !cash) { alert('Customer does not have enough money to purchase the item'); + return; } else if (cash === totalPriceOnCart) { changeDue.textContent = 'No change due - customer paid with exact cash'; + return; // remove this soon + } else if (cash > totalPriceOnCart) { + const difference = cash - totalPriceOnCart; + alert('proceeded to calculate change'); + calculateChange(difference); } } @@ -61,11 +99,9 @@ function updatePriceIndicator() { function calculateCartItems() { let temporaryPrice = 0; Object.entries(cart).forEach(([product, price]) => { - console.log(`${price} + ${temporaryPrice}`); temporaryPrice += price * productsPrice[product]; }) totalPriceOnCart = temporaryPrice; - console.log(`total price on cart: $${totalPriceOnCart}`); } // ===== WHEN PLUS BTN OR MINUS BTN IS CLICKED ===== @@ -105,7 +141,6 @@ function plusOrMinusBtnIsClicked(event) { } } } - console.log(cart); } // ===== LISTEN FOR UPDATE QUANTITY =====