Tell us what’s happening:
Hi, i need help here, it seems to work well in the browser but fail at free code comp.
Your code so far
const purchaseBtn = document.getElementById('purchase-btn');
const cash = document.getElementById('cash');
const changeDue = document.getElementById('change-due');
const checkCashRegister = () => {
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]
];
if(cash.value < price) {
alert('Customer does not have enough money to purchase the item');
} else if (cash.value == price) {
changeDue.textContent = 'No change due - customer paid with exact cash';
} else {
let currencyUnit = {
"PENNY": .01,
"NICKEL": .05,
"DIME": .1,
"QUARTER": .25,
"ONE": 1,
"FIVE": 5,
"TEN": 10,
"TWENTY": 20,
"ONE HUNDRED": 100
}
let change = cash.value - price;
let totalCID = Number(cid.reduce((sum, element) => sum + element[1],0).toFixed(2));
if (totalCID < change) {
return changeDue.textContent = 'Status: INSUFFICIENT_FUNDS';
} else if( totalCID == change) {
return changeDue.textContent = 'Status: CLOSED';
} else {
let changedArr = [];
for(let i = cid.length - 1; i >= 0; i--) {
let currencyUnitName = cid[i][0];
let currencyUnitValueTotal = cid[i][1];
let currencyUnitValue = currencyUnit[currencyUnitName];
let currencyUnitAmount = Number((currencyUnitValueTotal / currencyUnitValue).toFixed(0));
let currencyUnitToReturn = 0;
while (change >= currencyUnitValue && currencyUnitAmount > 0) {
change -= currencyUnitValue;
change = Number(change.toFixed(2));
currencyUnitAmount--;
currencyUnitToReturn++;
}
if(currencyUnitToReturn > 0) {
changedArr.push([currencyUnitName, currencyUnitToReturn * currencyUnitValue]);
}
}
if (change > 0) {
return changeDue.textContent = 'Status: INSUFFICIENT_FUNDS';
} else {
return changeDue.textContent = 'Status: OPEN';
}
}
}
}
purchaseBtn.addEventListener('click', checkCashRegister);
<!-- file: index.html -->
/* file: styles.css */
/* file: script.js */
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/123.0.0.0 Safari/537.36
Challenge Information:
Build a Cash Register Project - Build a Cash Register