Tell us what’s happening:
I’ve been trying to crack this problem for around a week and this is how much progress I have made so far. The parts I’m struggling to figure out are as follows.
#1: When change is owed, the cid won’t subtract the change from its drawer at all and update based off of that. The only idea that I have come up for this is amount -= currency.value. Does it have something to do with this or is there something else I’m not thinking about?
#2:I’m struggling to figure out why my cash register function isn’t correctly calculating the right denomination and it’s corresponding value in my change-due element. For example, if the change is 50 cents, then the textContent for the element will display PENNY: 0.49 instead of QUARTER: 0.50. For larger amounts of change like $20 it’ll say ONE: 9 instead of TWENTY: 20.
These are the main two problems that I can think of. If you see any other major issues that should be addressed then please let me know.
Your code so far
<!-- file: index.html -->
<input id="cash">
<div id="change-due"></div>
<button id="purchase-btn">Purchase</button>
<script src="script.js"></script>`
/* file: styles.css */
/* 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]
];
let currencyUnit = [
{name: "ONE HUNDRED", value: 100.0},
{name: "TWENTY", value: 20.0},
{name: "TEN", value: 10.0},
{name: "FIVE", value: 5.0},
{name: "ONE", value: 1.0},
{name: "QUARTER", value: 0.25},
{name: "DIME", value: 0.1},
{name: "NICKEL", value: 0.05},
{name: "PENNY", value: 0.01},
];
const cashInput = document.getElementById('cash');
const changeDue = document.getElementById('change-due');
const purchase = document.getElementById('purchase-btn');
purchase.addEventListener('click', exceptions);
purchase.addEventListener('click', checkCashRegister);
function exceptions() {
if (parseFloat(cashInput.value) < price) {
alert("Customer does not have enough money to purchase the item");
} else if (parseFloat(cashInput.value) === price) {
changeDue.textContent = "No change due - customer paid with exact cash";
} else {
checkCashRegister()
}
}
function checkCashRegister() {
let change = parseFloat(cashInput.value - price);
let roundedChange = parseFloat(change.toFixed(2));
let sumCid = 0;
for (let element of cid) {
sumCid += element[1]
}
let roundedCidSum = parseFloat(sumCid.toFixed(2));
if (roundedCidSum < roundedChange) {
changeDue.textContent = "Status: INSUFFICIENT_FUNDS"
return;
}
if (roundedCidSum === roundedChange) {
changeDue.textContent = `Status: CLOSED ${denomination}: ${amount}`;
return;
}
cid = cid.reverse();
for (let i = 0; i < cid.length; i++) {
let denomination = cid[i][0];
let amount = cid[i][1];
let currency = currencyUnit.find(unit => unit.name === denomination);
let value = 0;
const currencyValue = parseFloat(Object.values(currencyUnit));
const currencyName = Object.keys(currencyUnit);
while (roundedChange >= currency.value && amount > 0) {
roundedChange -= currency.value;
amount -= currency.value;
value += currency.value;
let roundedValue = parseFloat(value.toFixed(2))
changeDue.textContent = `Status: OPEN ${denomination} $${roundedValue}`
if (roundedChange === 0 ) {
break;
}
}
}
}
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 Edg/122.0.0.0
Challenge Information:
Build a Cash Register Project - Build a Cash Register