Tell us what’s happening:
The code seems to be working, I just can’t understand why the test is not accepting it
Your code so far
<!-- file: index.html -->
const cash=document.querySelector("#cash");
const change_due=document.getElementById("change-due");
const btn=document.getElementById("purchase-btn");
const change_due_box=document.getElementById("change-due-box");
const cid_txt=document.querySelector(".register_cont_b");
const total_txt=document.querySelector(".register_cont_a");
let price = 19.5;
/*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 cid = [
['PENNY', 0.5],
['NICKEL', 0],
['DIME', 1],
['QUARTER', 1],
['ONE', 1],
['FIVE', 0],
['TEN', 0],
['TWENTY', 0],
['ONE HUNDRED', 0]
];
let cid_cent=[
['PENNY', 0],
['NICKEL', 0],
['DIME', 0],
['QUARTER', 0],
['ONE', 0],
['FIVE', 0],
['TEN', 0],
['TWENTY', 0],
['ONE HUNDRED', 0]
];
let change = 0;
let change_arr= [0,0,0,0,0,0,0,0,0];
let arr_val= [1,5,10,25,100,500,1000,2000,10000]
//convert to cent
for(let i=8; i>-1;i--){
cid_cent[i][1]=Math.round(cid[i][1]*100)
}
let price_c=Math.round(price*100);
//--
const calculate_change = (c) => {
change_due_box.style.display="block";
change=c-price_c;
for(let i = 8; i>-1;i--){console.log(change);
while(change-arr_val[i]>=0&&cid_cent[i][1]>0){
cid_cent[i][1]=cid_cent[i][1]-arr_val[i];
change-=arr_val[i];
change_arr[i]=change_arr[i]+1};
};
check_funds(change);
}
const cbox_update = () => {
change_due.innerHTML=`<strong>Status: ${isClose()}</strong>`;
for(let i=8; i>-1;i--){
change_arr[i]>0?change_due.innerHTML+=
`<div>${cid[i][0]}: $${change_arr[i]*arr_val[i]/100}</div>`:false;
};
}
const isClose = () => {
return cid_cent.every(row=>row[1]==0)?"CLOSED":"OPEN";
}
const check_funds = (c) => {
if(c>0){
change_due.textContent="Status: INSUFFICIENT_FUNDS";
}
else{
cbox_update();
update();}
}
const click = () => {
change_arr= [0,0,0,0,0,0,0,0,0];
if(cash.value<price){
alert("Customer does not have enough money to purchase the item")
}
else if(cash.value==price){
change_due.textContent="No change due - customer paid with exact cash";
change_due_box.style.display="block";
}
else
{
calculate_change(cash.value*100);
}
}
const update = () =>{
for(let i=8; i>-1;i--){
cid[i][1]=cid_cent[i][1]/100;}
total_txt.innerHTML=`<strong>~ Total: $${price} ~</strong>`
cid_txt.innerHTML=
`<p><strong>Change In Drawer:</strong><br>
Pennies: $${cid[0][1]}<br>
Nickels: $${cid[1][1]}<br>
Dimes: $${cid[2][1]}<br>
Quarters: $${cid[3][1]}<br>
Ones: $${cid[4][1]}<br>
Fives: $${cid[5][1]}<br>
Tens: $${cid[6][1]}<br>
Twenties: $${cid[7][1]}<br>
Hundreds: $${cid[8][1]}<br></p>
`;
}
window.addEventListener("load",update);
btn.addEventListener("click",click);
/* file: styles.css */
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/132.0.0.0 Safari/537.36
Challenge Information:
Build a Cash Register Project - Build a Cash Register