I’m currently doing the cash register project and it took me a good amount of time to get where I am now. Only to find out my code doesn’t work with the website but when I test the code on my cmder it seems to work fine.
Here is my code and if anyone can help me get by this problem ill be super grateful, going to learn more in udemy for now.
let denomValue = [
{ name: 'ONE HUNDRED', val: 100.00 },
{ name: 'TWENTY', val: 20.00 },
{ name: 'TEN', val: 10.00 },
{ name: 'FIVE', val: 5.00 },
{ name: 'ONE', val: 1.00 },
{ name: 'QUARTER', val: 0.25 },
{ name: 'DIME', val: 0.10 },
{ name: 'NICKEL', val: 0.05 },
{ name: 'PENNY', val: 0.01 }
];
let checkCashRegister = function (price, cash, cid) {
let change = cash - price
// transform cid into one object (number) to work with
let cashDrawer = cid.reduce(function(acc,curr){
acc.total += curr[1];
acc[curr[0]] = curr[1];
return acc;
}, { total: 0 });
// Check for cash first
// made status equal three different things depending on the if
if (cashDrawer < change) {
status = 'INSUFFICIENT_FUNDS'
} else if (cashDrawer === change) {
status = 'CLOSED'
} else {
status = 'OPEN'
}
// make a loop to go through the denom and add value from Greatest to smallest.
let result = denomValue.reduce(function (acc, curr) {
let value = 0
while (cashDrawer[curr.name] > 0 && change >= curr.val) {
change -= curr.val
cashDrawer[curr.name] -= curr.val
value += curr.val
// math round to deal with run off numbers.
change = Math.round(change * 100) / 100
}
// this if statement is basically saying if we added to value print down what we added. with a push statement
if (value > 0) {
acc.push(['["' + curr.name + '"', value + ']'])
}
return acc
}, []);
// returned all into a simple template string
return console.log((`{status: "${status}", change: [${result}]}`))
}
checkCashRegister(2.50, 10, [["PENNY", 1.01], ["NICKEL", 2.05], ["DIME", 3.1], ["QUARTER", 4.25], ["ONE", 90], ["FIVE", 55], ["TEN", 20], ["TWENTY", 60], ["ONE HUNDRED", 100]])