Tell us what’s happening:
My algorithm is working and my answers match the answers requested by the questions, but when I run the test, questions after 12 don’t get checked, is there a trick or some rule that should be followed?
Your code so far
<!-- file: index.html -->
let price = 19.5;
let cid = [["PENNY", 0.01], ["NICKEL", 0], ["DIME", 0], ["QUARTER", 0], ["ONE", 0], ["FIVE", 0], ["TEN", 0], ["TWENTY", 0], ["ONE HUNDRED", 0]]
let changeLog={};
const currencyUnit = [
["Penny", 0.01 ],
["Nickel", 0.05 ],
["Dime", 0.1 ],
["Quarter", 0.25 ],
["Dollar", 1 ],
["Five Dollars", 5 ],
["Ten Dollars", 10 ],
["Twenty Dollars", 20 ],
["One Hundred Dollars", 100],
]
let cash = document.getElementById('cash');
const purchaseBtn = document.querySelector('#purchase-btn');
const changeDue = document.querySelector('#change-due');
const cashDrawer = document.querySelector('#cash-drawer');
const priceP = document.querySelector("#price")
let cashInDrawer = totalDrawerMoney();
window.onload = () => {
updateDrawer();
updatePrice()
}
purchaseBtn.addEventListener('click', () => {
changeDue.innerHTML = ""
if (cash.value===""){
alert("Please enter the cash amount");
} else if(cash.value < price){
alert("Customer does not have enough money to purchase the item");
}else if (parseFloat(cash.value) === parseFloat(price)){
statusUpdate("No change due - customer paid with exact cash")
}else calcChange();
})
function calcChange(){
cash = parseFloat(cash.value) ;
price = parseFloat(price.toFixed(2));
let change = cash - price;
if (cash > price + cashInDrawer){
statusUpdate("Status: INSUFFICIENT_FUNDS");
}
else{
currencyUnit.slice().reverse().forEach(
([key , value],index)=>{
value = parseFloat(value.toFixed(2))
let flag = true
while(flag){
if (change >= value && cid[cid.length-index-1][1]!=0){
key = cid[cid.length-index-1][0]
change = parseFloat((change-value).toFixed(2))
changeLog[key] ? changeLog[key]+=value : changeLog[key]=value
cid[cid.length-index-1][1]-=value
cashInDrawer -=value
}
else if(change<value || cid[cid.length-index-1][1]==0 ){
flag = false
}
}
}
)
if (cashInDrawer==0){
statusUpdate("Status: CLOSED ")
} else if (change!=0){
statusUpdate("Status: Not enough change ")
}else{
statusUpdate("Status: OPEN ")
changeLog = Object.entries(changeLog)
changeLog.forEach(([k,v])=>{
changeDue.innerHTML +=`<p>${k}: $${v}<\p>`
updateDrawer()
})
}
}
}
function totalDrawerMoney (){
let arr = []
for (const [key,value] of cid) {
arr.push(value);
}
return parseFloat(arr.reduce((a, b) => a + b, 0).toFixed(2));
}
function updateDrawer(){
cashDrawer.innerHTML = `Change in drawer: `
for (const [key,value] of cid){
let string = key.toLowerCase()
string = string[0].toUpperCase() + string.slice(1)
cashDrawer.innerHTML += `${string} : $${value}\n`
}
}
function updatePrice(){
priceP.textContent = `Total: $${price}`
}
function statusUpdate(str){
changeDue.innerHTML += `${str}`
}
/* 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/131.0.0.0 Safari/537.36
Challenge Information:
Build a Cash Register Project - Build a Cash Register