Tell us what’s happening:
Code seems to satisfy the test conditions, but fCC does not pass the tests.
Please help. This is very frustrating.
Thanks.
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" />
<link rel='stylesheet' href='styles.css'>
</head>
<body>
<div id="price-item"></div>
<div id="change-due"></div>
<label for="cash">Enter Cash
<input id="cash" value="cash" type="number"></input></label>
<button id="purchase-btn">Purchase</button>
<div id="cash-in-drawer"></div>
<script src='script.js'></script>
</body>
</html>
/* file: script.js */
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],
['ONE HUNDRED', 100]
];
const customerCash = document.getElementById("cash");
const purchaseBtn = document.getElementById("purchase-btn");
const changeDue = document.getElementById("change-due");
const cashInDrawer = document.getElementById("cash-in-drawer");
const priceItem = document.getElementById("price-item");
const pricePenny = Math.round(price*100);
const cidPenny = cid.map(cur=>[cur[0],Math.round(cur[1]*100)]);
const custChange = cid.map(cur=>[cur[0],0]);
const curPenny = [1,5,10,25,100,500,1000,2000,10000];
priceItem.innerHTML = `<div>Price: ${price}</div><br>`
const updateCID = () => {
cashInDrawer.innerHTML = `<br><div><b>Cash in Drawer:</b></div><br>`;
cid.forEach((cur)=>{
cashInDrawer.innerHTML += `
<div>${cur[0]}: ${cur[1]}</div>`;
});
}
const updateStatus = (changeRem) => {
if(changeRem === 0) {
if(cid.every((el)=>el===0)) {
changeDue.innerHTML = `Status: CLOSED<br>`
} else { changeDue.innerHTML = `Status: OPEN<br>`
}
for(let i=custChange.length-1; i>=0; i--) {
cid[i][1] = cidPenny[i][1]/100;
if(custChange[i][1]>0) {changeDue.innerHTML += `${custChange[i][0]}: $${custChange[i][1]*(curPenny[i]/100)}<br>`;}
}
} else {
changeDue.innerHTML = `Status: INSUFFICIENT FUNDS<br>`;
for(let i=cid.length-1; i>=0; i--) {
cidPenny[i][1] = cid[i][1]*100;}
}
}
updateCID();
const checkInput = () => {
const custCashPenny = Number(customerCash.value*100);
const custCash = Number(customerCash.value);
let changePenny = custCashPenny - pricePenny;
if(custCash < price) {
alert("Customer does not have enough money to purchase the item");
} else if(custCash === price) {
changeDue.innerHTML = `<br>No change due - customer paid with exact cash<br>`;
} else {
for(let i=cid.length-1; i>=0; i--) {
custChange[i][1] =0;
if(changePenny >= curPenny[i]) {
custChange[i][1] = Math.floor(changePenny/curPenny[i]);
if(custChange[i][1]*curPenny[i] <= cidPenny[i][1]) {
changePenny = changePenny%curPenny[i];
cidPenny[i][1] -= custChange[i][1]*curPenny[i];
} else {
custChange[i][1] = Math.floor(cidPenny[i][1]/curPenny[i]);
changePenny -= cidPenny[i][1];
cidPenny[i][1] = 0;
}
}
}
updateStatus(changePenny);
updateCID();
}
customerCash.value = "";
}
purchaseBtn.addEventListener("click",checkInput);
customerCash.addEventListener("keydown",(e)=>{
if(e.key=== "Enter") {
checkInput();
};
})
/* 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.3.1 Safari/605.1.15
Challenge Information:
Build a Cash Register Project - Build a Cash Register