Tell us what’s happening:
Every single other test is succeeding, but these 3 I mentioned in the title are very inconsistent. In one run, 2 of these might pass and the other might fail (in most cases for me it’s been #17), in another run it might be a combination of 2 of these tests that fail instead. I figure it’s must a bug, right? Do I just have to keep running the tests and pray to succeed?
Your code so far
<!-- file: index.html -->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Cash Register</title>
<link rel="stylesheet" href="style.css">
</head>
<div>
<input id="cash" />
<button id="purchase-btn">Purchase</button>
</div>
<p id="price">Total: </p>
<p id>Change due:</p>
<p id="change-due"></p>
<body>
<script src="./script.js"></script>
</body>
</html>
/* file: script.js */
let price = 19.5;
let cid = [["PENNY", 0.5], ["NICKEL", 0], ["DIME", 0], ["QUARTER", 0], ["ONE", 0], ["FIVE", 0], ["TEN", 0], ["TWENTY", 0], ["ONE HUNDRED", 0]];
// In pennies to avoid potential floating point errors
const currencyUnit = {
"PENNY": 1,
"NICKEL": 5,
"DIME": 10,
"QUARTER": 25,
"ONE": 100,
"FIVE": 500,
"TEN": 1000,
"TWENTY": 2000,
"ONE HUNDRED": 10000
};
const total = document.getElementById("price");
const purchaseBtn = document.getElementById("purchase-btn");
const changeDue = document.getElementById("change-due");
const isChangeEnough = (cashInRegister, changeInCents) => {
let availableChange = 0;
for (const [curr, val] of cashInRegister) {
console.log(val)
if(currencyUnit[curr] <= changeInCents) {
availableChange += val * 100;
} else {
break;
}
}
console.log("Available change is ", availableChange, ", and change in cents is ", changeInCents)
return availableChange >= changeInCents;
}
total.innerText += " " + price;
purchaseBtn.addEventListener("click", () => {
let totalChange = 0;
for (const item of cid) {
totalChange += item[1] * 100;
}
const cash = document.getElementById("cash").value;
if (!cash) {
return;
}
//convert to cents to maintain consistency
const cashVal = Number(cash) * 100;
// Not enough cash for purchase
if (cashVal < (price * 100)) {
alert("Customer does not have enough money to purchase the item")
// Cash matches price - no change needed
} else if (cashVal === (price * 100)) {
changeDue.innerText = "No change due - customer paid with exact cash";
// Change needs to be given
} else {
let change = cashVal - (price * 100);
// Insufficient change
console.log(change)
if (change > totalChange || !isChangeEnough(cid, change)) {
changeDue.innerText = "Status: INSUFFICIENT_FUNDS";
// Cash in register is exactly the same as change needed to be given
} else if (change === totalChange) {
changeDue.innerText = "Status: CLOSED";
for (let i = cid.length - 1; i >= 0; i--) {
if (cid[i][1] !== 0){
changeDue.innerText += ` ${cid[i][0]}: \$${cid[i][1]}`;
}
}
// Change with cash remaining in the register
} else {
const changeGiven = [];
// Copy original cid array and reverse it
const cidReversed = [...cid].reverse();
for (let [currency, amount] of cidReversed) {
const unitValue = currencyUnit[currency];
let currencyAvailable = amount * 100;
let currencyUsed = 0;
while (change >= unitValue && currencyAvailable > 0) {
change -= unitValue;
currencyAvailable -= unitValue;
currencyUsed += unitValue;
}
if (currencyUsed > 0) {
changeGiven.push([currency, currencyUsed / 100]);
}
}
changeDue.innerText = "Status: OPEN";
for (let [currency, amount] of changeGiven) {
changeDue.innerText += ` ${currency}: \$${amount}`;
}
}
}
});
Your browser information:
User Agent is: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:142.0) Gecko/20100101 Firefox/142.0
Challenge Information:
Build a Cash Register Project - Build a Cash Register