Tell us what’s happening:
I believe I have come up with a sound solution to the Cash Register problem; however, I can’t pass it even though its outputting the correct values. I think maybe its related to the spaces between brackets in the change array … but I can’t figure out why that is happening or if it’s even the reason.
should return {status: "OPEN", change: [["QUARTER", 0.5]]}
my code returns { status: 'OPEN', change: [ [ 'QUARTER', '0.5' ] ] }
**Your code so far**
function checkCashRegister(price, cash, cid) {
let changeDue = (cash - price); //Need to know how much change we need to give back
let remainingDue = changeDue; //While we are giving change we should know how much more change we need to give after giving out some of the change
let changeGiven = 0; //We should know how much chnage we have given out at any time
const originalDrawer = JSON.parse(JSON.stringify(cid)).reverse(); //keep original drawer for comparing
const cashInDrawer = JSON.parse(JSON.stringify(originalDrawer)); //working drawer
const output = {status: '', change: []}; //drawer output status and change count
let cidTotal = cashInDrawer.reduce((sum, money) => sum + money[1], 0); //Total cash in the drawer
//Set values for each unit of currency
var values = {
"PENNY": 0.01,
"NICKEL": 0.05,
"DIME": 0.10,
"QUARTER": 0.25,
"ONE": 1.00,
"FIVE": 5.00,
"TEN": 10.00,
"TWENTY": 20.00,
"ONE HUNDRED": 100.00
}
//Use this function to make change
function makeChange(){
// if we have enough change in the drawer
if (changeDue < cidTotal) {
output.status = "OPEN"; //set status to OPEN
//Loop through the cash in the drawer
for (let a = 0; a < cashInDrawer.length; a++){
//do this as long as we have not remaining change to be given
if (remainingDue > 0) {
//while each unit amount is dividable by its currency value with no remainder and the amount of the unit is not 0
while (remainingDue.toFixed(2)/values[cashInDrawer[a][0]] >=1 && cashInDrawer[a][1]>0){
cashInDrawer[a][1] -= values[cashInDrawer[a][0]]; //subtract the unit value from total value of unit in the drawer
remainingDue -= values[cashInDrawer[a][0]]; //subtract unit value from remaining change
changeGiven += values[cashInDrawer[a][0]]; //add unit value to change handed out
}
}
//if the cash between the drawers is not zero push units and values to output
if (originalDrawer[a][1] - cashInDrawer[a][1] !== 0){
output.change.push([cashInDrawer[a][0],Math.round((originalDrawer[a][1] - cashInDrawer[a][1])*100)/100]);
}
}
//If the change in the drawer is exact status to closed and output cashInDrawer
} else if (changeDue == cidTotal) {
output.status = "CLOSED";
for (let b = cashInDrawer.length-1; b >= 0; b--){
output.change.push(cashInDrawer[b][0],cashInDrawer[b][1]);
}
//if the change due is more than whats in the drawer set status to insufficient funds
} else if (changeDue > cidTotal) {
output.status = "INSUFFICIENT FUNDS";
}
return output;
}
console.log({changeDue});
console.log({remainingDue});
console.log({changeGiven});
console.log(makeChange());
return makeChange();
}
checkCashRegister(19.5, 20, [["PENNY", 1.01], ["NICKEL", 2.05], ["DIME", 3.1], ["QUARTER", 4.25], ["ONE", 90], ["FIVE", 55], ["TEN", 20], ["TWENTY", 60], ["ONE HUNDRED", 100]]);
**Your browser information:**
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36
Challenge: Cash Register
Link to the challenge: