Tell us what’s happening:
Hello,
I have an issue with the “Build a Cash Register” task.
My code works, but the tests in JavaScript are not passing.
Can you fix this?
Thank you.
Your code so far
<!-- file: index.html -->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
</head>
<body>
<h1>Cash provided by the customer :</h1>
<input id="cash">
<div id="change-due"></div>
<button id="purchase-btn">...</button>
<script type="module" src="script.js"></script>
</body>
</html>
/* file: script.js */
let price = 3.26;
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]
];
let cu = [
['PENNY', 0.01],
['NICKEL', 0.05],
['DIME', 0.1],
['QUARTER', 0.25],
['ONE', 1],
['FIVE', 5],
['TEN', 10],
['TWENTY', 20],
['ONE HUNDRED', 100],
];
let cash = document.getElementById("cash");
const changeDue = document.getElementById("change-due");
const purchaseBtn = document.getElementById("purchase-btn");
const cashDesk = cashCustomer => {
changeDue.textContent= "";
if (cashCustomer<price) {
alert("Customer does not have enough money to purchase the item");
}
else if (cashCustomer===price) {
changeDue.textContent="No change due - customer paid with exact cash";
}
else if (cashCustomer>cashAmount(cid)){
changeDue.textContent="Status: INSUFFICIENT_FUNDS";
}
else if (cashCustomer===cashAmount(cid)){
changeDue.textContent="Status: CLOSED";
cashRegister(cashCustomer-price);
}
else {
changeDue.textContent="Status: OPEN";
cashRegister(cashCustomer-price);
}
}
const cashRegister = cashReturn => {
let restCash = Math.floor(100*cashReturn);
let indexAmount = 0;
let nbBills = 0;
let sumBills = 0;
let status = "";
for(let indexAmount=8;indexAmount>=0;indexAmount--) {
nbBills = Math.floor(restCash / (100*cu[indexAmount][1]));
if (nbBills) {
sumBills = nbBills*100*cu[indexAmount][1];
if (sumBills>(100*cid[indexAmount][1])){
sumBills=100*cid[indexAmount][1];
}
status += ` ${cu[indexAmount][0]}: $${sumBills/100}`;
restCash -= sumBills
}
}
changeDue.textContent+=status;
}
const cashAmount = cash => cash.reduce((acc,e) => acc+e[1], 0);
purchaseBtn.addEventListener("click", ()=>{
const regex = /[\d.]+/;
if (regex.test(cash.value)){
cashDesk(Number(cash.value));
}
});
/* file: styles.css */
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:133.0) Gecko/20100101 Firefox/133.0
Challenge Information:
Build a Cash Register Project - Build a Cash Register