Locally, my cash register seems to work, but apparently no tests are passing. I’d appreciate any help thanks!
HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Cash Register Project</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<header> CASH REGISTER PROJECT</header>
<main>
<div id="change-due">
<p id="status" class="hidden"></p>
<p id="hundred-p" class="hidden"></p>
<p id="twenty-p" class="hidden">/p>
<p id="five-p" class="hidden"></p>
<p id="one-p" class="hidden"></p>
<p id="quarter-p" class="hidden"></p>
<p id="dime-p" class="hidden"></p>
<p id="nickel-p" class="hidden"></p>
<p id="penny-p" class="hidden"></p>
</div>
<label for="cash">Enter cash from customer:</label>
<input type="number" name="cash" id="cash">
<button id="purchase-btn">Purchase Item</button>
<div id="total">Total price: $ </div>
<div id="available-cash">
<div id="penny">Pennies: <span>$1.01 </span></div>
<div id="nickel">Nickels: <span>$2.05 </span></div>
<div id="dime">Dimes: <span>$3.1 </span></div>
<div id="quarter">Quarters: <span>$4.25 </span></div>
<div id="one">Ones: <span>$90 </span></div>
<div id="five">Fives: <span>$55 </span></div>
<div id="ten">Tens: <span>$20 </span></div>
<div id="twenty">Twenties: <span>$60 </span></div>
<div id="hundred">Hundreds: <span>$100 </span></div>
</div>
</main>
<script src="script.js"></script>
</body>
</html>
JAVASCRIPT
let price = 1.87;
let cid = [
["PENNY", 1.01],
["NICKEL", 2.05],
["DIME", 3.1],
["QUARTER", 4.25],
["ONE", 90],
["FIVE", 55],
["TEN", 20],
["TWENTY", 60],
["HUNDRED", 100]
];
const availableCash = document.getElementById("available-cash")
const customerCash = document.getElementById("cash")
const totalPrice = document.getElementById("total")
const purchaseBtn = document.getElementById("purchase-btn")
const changeDue = document.getElementById("change-due")
const statusP = document.getElementById("status")
let arrValues = [];
cid.forEach((arr) => {
arrValues.push(arr[1])
});
totalPrice.innerText += price
const cashSum = arrValues.reduce((a, b) => a + b).toFixed(2);
const updateResult = (value, id) => {
let correctedId = id.toLowerCase().replace(/\s/g, "")
document.getElementById(`${correctedId}-p`).textContent = `${id}: $${value.toFixed(2).replace(/\.00/g, "")}`
document.getElementById(`${correctedId}-p`).style.display = "block"
}
purchaseBtn.addEventListener("click", () => {
let customerCashValue = parseFloat(customerCash.value).toFixed(2);
let changeToCalculate = (customerCashValue - price).toFixed(2);
if (customerCashValue < price) {
alert("Customer does not have enough money to purchase the item");
} else if (customerCashValue == price) {
changeDue.textContent = "No change due - customer paid with exact cash"
} else if (customerCashValue == cashSum) {
changeCalculator(changeToCalculate, customerCashValue)
statusP.style.display = "block";
statusP.innerText = "Status: CLOSED";
} else {
changeCalculator(changeToCalculate, customerCashValue)
}
}
)
const checkInsufficientFunds = (changeToCalculate, cashSum) => {
if (cashSum < changeToCalculate) {
statusP.innerText = "Status: INSUFFICIENT_FUNDS"
statusP.style.display = "block";
}
}
const changeCalculator = (changeToCalculate, customerCashValue) => {
checkInsufficientFunds(changeToCalculate);
statusP.innerText = "Status: OPEN";
statusP.style.display = "block";
while (changeToCalculate > 0) {
if (changeToCalculate >= 100 && cid[8][1]) {
const multiplier = Math.floor(changeToCalculate/ 100);
const value = 100 * multiplier
customerCashValue -= (100 * multiplier);
changeToCalculate -= (100 * multiplier);
cid[8][1] -= (100 * multiplier);
updateCashInRegister(cid);
updateResult(value, cid[8][0])
}
else if (changeToCalculate >= 20 && cid[7][1] > 0) {
const multiplier = Math.floor(changeToCalculate/ 20);
const value = 20 * multiplier;
customerCashValue -= (20 * multiplier);
changeToCalculate -= (20 * multiplier);
cid[7][1] -= (20 * multiplier);
updateCashInRegister(cid);
updateResult(value, cid[7][0])
}
else if (changeToCalculate >= 10 && cid[6][1] > 0) {
const multiplier = Math.floor(changeToCalculate/ 10);
const value = 10 * multiplier;
customerCashValue -= (10 * multiplier);
changeToCalculate -= (10 * multiplier);
cid[6][1] -= (10 * multiplier);
updateCashInRegister(cid);
updateResult(value, cid[6][0])
}
else if (changeToCalculate >= 5 && cid[5][1] > 0) {
const multiplier = Math.floor(((changeToCalculate/ 5)));
const value = 5 * multiplier;
customerCashValue -= (5 * multiplier);
changeToCalculate -= (5 * multiplier);
cid[5][1] -= (5 * multiplier);
updateCashInRegister(cid);
updateResult(value, cid[5][0])
}
else if (changeToCalculate >= 1 && cid[4][1] > 0) {
const multiplier = Math.floor(changeToCalculate/ 1);
const value = 1 * multiplier;
customerCashValue -= (1 * multiplier);
changeToCalculate -= (1 * multiplier);
cid[4][1] -= (1 * multiplier);
updateCashInRegister(cid);
updateResult(value, cid[4][0])
}
else if (changeToCalculate >= 0.25 && cid[3][1] > 0) {
const multiplier = Math.floor(changeToCalculate/ 0.25);
const value = 0.25 * multiplier;
customerCashValue -= (0.25 * multiplier);
changeToCalculate -= (0.25 * multiplier);
cid[3][1] -= (0.25 * multiplier);
updateCashInRegister(cid);
updateResult(value, cid[3][0])
}
else if (changeToCalculate >= 0.1 && cid[2][1] > 0) {
const multiplier = Math.floor(changeToCalculate/ 0.1);
const value = 0.1 * multiplier;
customerCashValue -= (0.1 * multiplier);
changeToCalculate -= (0.1 * multiplier);
cid[2][1] -= (0.1 * multiplier);
updateCashInRegister(cid);
updateResult(value, cid[2][0])
} else if (changeToCalculate >= 0.05 && cid[1][1] > 0) {
const multiplier = (changeToCalculate/ 0.05);
const value = 0.05 * multiplier;
customerCashValue -= (0.05 * multiplier);
changeToCalculate -= (0.05 * multiplier);
cid[1][1] -= (0.05 * multiplier);
updateCashInRegister(cid);
updateResult(value, cid[1][0])
} else if (changeToCalculate >= 0.01 && cid[0][1] > 0) {
const multiplier = (changeToCalculate / 0.01);
const value = 0.01 * multiplier;
customerCashValue -= (0.01 * multiplier);
changeToCalculate -= (0.01 * multiplier);
cid[0][1] -= (0.01 * multiplier);
updateCashInRegister(cid);
updateResult(value, cid[0][0])
}
}
}
const updateCashInRegister = cid => {
cid.forEach((arr) => {
let id = arr[0].toLowerCase().replace(/\s/g, "")
document.querySelector(`#${id} span`).innerText = `$${arr[1].toFixed(2).replace(/\.00/g, "")}`;
});
console.log(cid)
}