Tell us what’s happening:
Hi:) could someone please tell me why my code is not passing some of the tests.
When I try the tests manually everything works, but when I try to run it, it says it doesn’t.
Please help!
(also I know my code is very messy right now and some things are redundant, but for the moment I am just concerned with passing. I hope it’s still readable)
Your code so far
<!-- file: index.html -->
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0" >
<link src="stylesheet" href="styles.css">
<title>cash register project</title>
</head>
<body>
<p id="price-display"></p>
<input id="cash"></input>
<p id="change-due"></p>
<button id="purchase-btn">purchase</button>
<p id="cid-display"></p>
<p id="test"></p>
<script src="script.js"></script>
</body>
</html>
/* file: script.js */
const cashInput = document.getElementById("cash");
const changeDue = document.getElementById("change-due");
const purchaseBtn = document.getElementById("purchase-btn");
const priceDisplay = document.getElementById("price-display");
const cidDisplay = document.getElementById("cid-display");
const test = document.getElementById("test");
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]
];
const currency = [
['ONE HUNDRED', 100],
['TWENTY', 20],
['TEN', 10],
['FIVE', 5],
['ONE', 1],
['QUARTER', 0.25],
['DIME', 0.10],
['NICKEL', 0.05],
['PENNY', 0.01],
];
priceDisplay.textContent = price;
const totalCid = cid.reduce((acc, array)=> acc + array[1],0).toFixed(2);
const cidReverse = cid.reverse();
const checkCash = () => {
const cash = parseFloat(cashInput.value);
if(cash < price){
alert("Customer does not have enough money to purchase the item")
changeDue.textContent = ""
}
else if(cash === price){
changeDue.textContent = "No change due - customer paid with exact cash";
}
else{
checkFunds(cash)
}
}
const checkFunds = (input) => {
let changeDueAmount = parseFloat(input - price).toFixed(2);
let changeDueAmount100 = changeDueAmount*100
if(changeDueAmount100 > (totalCid*100)){
changeDue.textContent = "Status: INSUFFICIENT_FUNDS"
}
else if(changeDueAmount === totalCid){
changeDue.textContent = "Status: CLOSED";
for(let i = 0; i < cid.length; i++){
if(parseFloat(cidReverse[i][1]) !== 0)
{ changeDue.textContent += ` ${cidReverse[i][0]}: $${cidReverse[i][1]}`}
};
}
else{
returnCash(changeDueAmount100)
}
}
const returnCash = (change) => {
let result = [...currency];
const originalChange = change/100;
for(let i = 0; i < currency.length; i++){
let returnMoney = 0;
let bills = cidReverse[i][1]/currency[i][1];
bills.toFixed(2);
while(change >= (currency[i][1]*100) && bills >= 1){
change -= (currency[i][1]*100);
returnMoney += currency[i][1];
bills--;
}
if(returnMoney > 0){
if(returnMoney - Math.floor(returnMoney) !== 0){
result[i][1]= returnMoney.toFixed(2)
result[i][1] = parseFloat(result[i][1])
}
else{
result[i][1] = returnMoney
}
}
else{
result[i][1] = returnMoney;
}
}
let sumResult = 0;
for(let i = 0; i < cid.length; i++){
sumResult+=result[i][1];
}
sumResult = sumResult.toFixed(2);
if(totalCid < originalChange||sumResult < originalChange){
changeDue.textContent = "Status: INSUFFICIENT_FUNDS"
}
else{
changeDue.textContent = 'Status: OPEN'
for(let i = 0; i < result.length; i++){
if(parseFloat(result[i][1]) !== 0){
changeDue.textContent += ` ${result[i][0]}: $${result[i][1]}`
}
}
}
}
purchaseBtn.addEventListener("click", checkCash);
/* file: styles.css */
Your browser information:
User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/18.1 Safari/605.1.15
Challenge Information:
Build a Cash Register Project - Build a Cash Register