]> git repositories - simple-cash-register.git/commitdiff
added an unfinished function, I think that's enough for today.
authorbochard <mail@tenkyuu.dev>
Sat, 1 Mar 2025 11:24:36 +0000 (19:24 +0800)
committerbochard <mail@tenkyuu.dev>
Sat, 1 Mar 2025 11:24:36 +0000 (19:24 +0800)
index.html
index.js

index f7bc147323d9b328ceaa6f368db44f884fcb1a54..124690bf2a46a8cd1bb607317a18d895d0936c0e 100644 (file)
@@ -16,7 +16,7 @@
     <div class="cash-register">
       <div class="cash-register-screen">
         <div class="price-indicator">Total: <span id="total-price">$0.00</span></div>
-        <div id="change-due"></div>
+        <div class="change-indicator">Change: <span id="change-due">$0.00</span></div>
       </div>
 
       <div id="product-list" class="product-list">
index c80ee779752f94f2d8ff105f88c85ac3aa57ddc7..eba34c74f3122743afdacff0283b7491d91be316 100644 (file)
--- 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 =====