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);
}
}
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 =====
}
}
}
- console.log(cart);
}
// ===== LISTEN FOR UPDATE QUANTITY =====