Hi (first post!)
I’m having trouble with the final test on the Cash Register project. The output seems to meet all the requirements, so I’m at a loss as to what’s preventing it from passing. I’d really appreciate any help on this!
My code is here (Please pardon the stark html. I’ve been focusing on function over form
)
HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8"/>
<meta name="viewport" content="width=device-width, initial-scale=0"/>
<link rel="stylesheet" href="./styles.css"/>
<title>Cash Register</title>
</head>
<body>
<h1>Cash Register</h1>
<strong>$</strong><input name="cash" id="cash" type="number"/>
<button id="purchase-btn">Purchase</button>
<div id="change-due"></div>
<script src="./script.js"></script>
</body>
<footer>by rybernate</footer>
</html>
CSS
body {
width: auto;
margin: 0;
align-content: center;
text-align: center;
}
input {
width: 10%;
min-width: 50px;
max-width: 100px;
}
footer{
display: block;
position: fixed;
bottom: 0;
width: 100%;
height: 20px;
align-content: center;
text-align: center;
color: #fff;
background-color: #000;
margin: 0;
font-family: Trebuchet, sans-serif;
font-size: .8rem;
}
JS
const purchaseBtn = document.getElementById("purchase-btn");
const changeDue = document.getElementById("change-due");
let cash = document.getElementById("cash");
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]
];
let monValues = [0.01, 0.05, 0.1, 0.25, 1, 5, 10, 20, 100];
const cidLength = cid.length - 1;
const mvLength = monValues.length -1;
let regTotal = 0;
let change = 0;
let currency = 0;
let changeMsg = "";
let tally = [];
const getRegTotal = () => {
for (let i=cidLength; i>=0; i--){
regTotal = Number(cid[i][1] * 100) + regTotal;
};
regTotal = regTotal / 100;
return;
};
const getNumberOfBillsAndCoins = () => {
for (let i=mvLength; i>=0; i--){
currency = Math.round((cid[i][1] * 100) / (monValues[i] * 100));
tally.unshift(currency);
};
return;
};
const getChange = () => {
let a = change*100;
for (let i=mvLength; i>=0; i--){
const b = (monValues[i]*100);
const c = (b * tally[i]);
const d = Math.floor(a / b);
if (d !== 0 && d >= tally[i] && tally[i] != 0) {
a = Math.round(a - c);
changeMsg += ` ` + cid[i][0] + `: $` + c / 100;
} else if (d !== 0 && d < tally[i]) {
a = Math.round(a - (d * b));
changeMsg += ` ` + cid[i][0] + `: $` + (d * b) / 100;
} else {
a = Math.round(a);
};
console.log(tally[i] + " " + b + " " + a + " " + c + " " + d + " " + regTotal);
};
if (a === 0 && change - regTotal !== 0) {
changeDue.textContent = `Status: OPEN ${changeMsg}`;
cash.value = "";
} else if (a === 0 && change - regTotal === 0) {
changeDue.textContent = `Status: CLOSED ${changeMsg}`;
cash.value = "";
} else {
changeDue.textContent = `Status: INSUFFICIENT_FUNDS`;
cash.value = "";
};
return;
};
const purchaseItem = () => {
getNumberOfBillsAndCoins();
getRegTotal();
if (Number(cash.value) < price) {
alert(`Customer does not have enough money to purchase the item`);
cash.value = "";
} else if (Number(cash.value) === price) {
changeDue.textContent = `No change due - customer paid with exact cash`;
cash.value = "";
} else if (change > regTotal) {
changeDue.textContent = `Status: INSUFFICIENT_FUNDS`;
cash.value = "";
} else {
finalSale();
};
return;
};
const finalSale = () => {
change = ((cash.value *100) - (price *100)) / 100;
getChange();
changeMsg = "";
cash.value = "";
regTotal = 0;
currency = 0;
tally = [];
return;
};
purchaseBtn.addEventListener("click",purchaseItem);
The test that’s failing is
When
priceis19.5, the value in the#cashelement is20,cidis[["PENNY", 0.5], ["NICKEL", 0], ["DIME", 0], ["QUARTER", 0], ["ONE", 0], ["FIVE", 0], ["TEN", 0], ["TWENTY", 0], ["ONE HUNDRED", 0]], and the#purchase-btnelement is clicked, the value in the#change-dueelement should be"Status: CLOSED PENNY: $0.5".


