Tell us what’s happening:
I have tried so many different functions to get this to work, I’ve stripped it down the the bare bones but there is always some condition that isn’t being satisfied. I believe the problem is with formatting but any help would be GREATLY appreciated.
Your code so far
WARNING
The challenge seed code and/or your solution exceeded the maximum length we can port over from the challenge.
You will need to take an additional step here so the code you wrote presents in an easy to read format.
Please copy/paste all the editor code showing in the challenge from where you just linked.
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]]
const cash = document.getElementById('cash')
const purchaseBtn = document.getElementById('purchase-btn')
const priceDisplay = document.getElementById('price-left')
const registerTotalDisplay = document.getElementById('change-title')
const displayCid = document.getElementById('change-in-drawer')
const status = document.getElementById('status-title')
const displayChangeDue = document.getElementById('change-due')
const formatResults = (status, change) => {
displayChangeDue.innerHTML += `<p>Status: ${status}</p>`
change.forEach(el => {
displayChangeDue.innerHTML += `<p>${el[0]}: $${el[1]}</p>`
})
}
const checkRegister = () => {
let payment = Number(cash.value)
if( payment < price ){
alert('Customer does not have enough money to purchase the item')
}
if( payment === price ){
console.log(Number(cash.value) === price)
displayChangeDue.innerHTML += `<p>No change due - customer paid with exact cash</p>`
return
}
let result = {
status: 'OPEN',
change: []
}
let changeDue = parseFloat((payment - price).toFixed(2))
let regTotal = parseFloat(cid.map(el => el[1]).reduce((t,c) => t + c).toFixed(2))
let denoms = [100, 20, 10, 5, 1, .25, .1, .05, .01]
let revCid = [...cid].reverse()
if( changeDue === regTotal ){
result.status = 'CLOSED'
}
if( changeDue > regTotal ){
result.status = 'INSUFFICIENT_FUNDS'
return result
}
for( let i = 0; i < denoms.length; i++ ){
if( denoms[i] <= changeDue && revCid[i][1] > 0 ){
let total = 0
let cashType = revCid[i][0]
while( denoms[i] <= changeDue && revCid[i][1] > 0 ){
total++
changeDue = parseFloat((changeDue -= denoms[i]).toFixed(2))
}
result.change.push([cashType, total * denoms[i]])
}
}
if( changeDue > 0 ){
result.status = 'INSUFFICIENT_FUNDS'
}
return result
}
purchaseBtn.addEventListener('click', () => {
const result = checkRegister()
console.log(result)
formatResults(result.status, result.change)
})
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36
Challenge Information:
Build a Cash Register Project - Build a Cash Register