hei, i have been working on my cash register and no matter what i do, the last test doest pass: below is my code; does anyone know what im doing wrong?
/* file: script.js */
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 cashRegister = (cash, price, cid) => {
const currency = {
PENNY: 0.01,
NICKEL: 0.05,
DIME: 0.1,
QUARTER: 0.25,
ONE: 1,
FIVE: 5,
TEN: 10,
TWENTY: 20,
"ONE HUNDRED": 100,
};
let changeDue = cash - price;
let changeArr = [];
let totalCID = Number(cid.reduce((a, b) => a + b[1], 0).toFixed(2));
if (totalCID < changeDue) {
return `Status: INSUFFICIENT_FUNDS`;
}
else {
for (let i = cid.length - 1; i >= 0; i--) {
let currencyName = cid[i][0];
let cashTotal = cid[i][1];
let currencyValue = currency[currencyName];
let currencyUnitAmount = Number((cashTotal / currencyValue).toFixed(0));
let currencyUnitToReturn = 0;
while (changeDue >= currencyValue && currencyUnitAmount > 0) {
changeDue -= currencyValue;
changeDue = Number(changeDue.toFixed(2));
currencyUnitAmount--;
currencyUnitToReturn++;
}
if (currencyUnitToReturn > 0) {
changeArr.push([`${currencyName}: $${currencyUnitToReturn * currencyValue}`]);
}
// console.log(changeArr);
}
if (changeDue > 0) {
return `Status: INSUFFICIENT_FUNDS`;
}
return changeDue === totalCID ?
`Status: CLOSED ${changeArr}`:`Status: OPEN ${changeArr}`;
}
// console.log(changeDue, totalCID);
};
const btn = document.getElementById("purchase-btn");
let price = 19.5;
const total = document.getElementById("total");
total.textContent = `$${price.toFixed(2)}`;
btn.addEventListener("click", (e) => {
e.preventDefault();
// constants
const changeDue = document.getElementById("change-due");
const cidText = document.getElementById("cid-text");
const cash = document.getElementById("cash");
const cashValue = parseFloat(cash.value);
const result = cashRegister(cashValue, price, cid);
// end of constants
if(cashValue < price){
alert('Customer does not have enough money to purchase the item')
}
else if(cashValue === price){
changeDue.textContent = `No change due - customer paid with exact cash`
}else{
changeDue.textContent = result
}
});
Your browser information:
User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36
Challenge Information:
Build a Cash Register Project - Build a Cash Register