# UPDATED: Cash Register Algorithm help

UPDATED: Cash Register Algorithm help
0

#1

Hello all - I’ve been working on this for a few days and I’m not sure if I have my logic correct or not?? My code at this point only passes the first test that it is supposed to return an object. I’m not sure what I’m missing at this point…if anyone can give me some advice, I’d really appreciate it!

Here’s my code so far (I’ll update it if I make changes):

UPDATED: Well, I’ve worked on this quite a bit…it’s not done yet, but am working on it!

``````function checkCashRegister(price, cash, cid) {
var change = {
status: null,
change: []
};
// Here is your change, ma'am.

var moneyTypesAndValues = [
{money: 'PENNY', value: 0.01},
{money: 'NICKEL', value: 0.05},
{money: 'DIME', value: 0.10},
{money: 'QUARTER', value: 0.25},
{money: 'ONE', value: 1.00},
{money: 'FIVE', value: 5.00},
{money: 'TEN', value: 10.00},
{money: 'TWENTY', value: 20.00},
{money: 'ONE HUNDRED', value: 100.00}
];

var currentDrawerTotal = 0;

function howMuchInDrawer(cid){
for(var i = 0; i<cid.length; i++){
currentDrawerTotal = currentDrawerTotal + cid[i][1];
}
return currentDrawerTotal;
}

function calculateChange(price,cash){
var returnChange = cash - price;

if(howMuchInDrawer(cid) < returnChange){
change.status = "INSUFFICIENT_FUNDS"
change.change = [];
}

if (howMuchInDrawer(cid) === returnChange){
change.status = "CLOSED";
change.change = cid;
}

if(howMuchInDrawer(cid) > returnChange){

//subtract from drawer totals here
var changeArray = [];
var tempVal = 0;
for(var j = 0; j<moneyTypesAndValues.length; j++){
if(currentDrawerTotal > 0 && returnChange >= moneyTypesAndValues[j].value){
returnChange = returnChange - moneyTypesAndValues[j].value;
currentDrawerTotal = currentDrawerTotal - moneyTypesAndValues[j].value;
tempVal = tempVal + moneyTypesAndValues[j].value;
returnChange = Math.round(returnChange * 100) / 100;
if(tempVal > 0){
changeArray.push([moneyTypesAndValues[j].money, returnChange]);
}
}
}
change.status = "OPEN";
change.change = changeArray.reverse();
}
}
calculateChange(price,cash);
console.log(change);
return change;
}

// Example cash-in-drawer array:
// [["PENNY", 1.01],
// ["NICKEL", 2.05],
// ["DIME", 3.1],
// ["QUARTER", 4.25],
// ["ONE", 90],
// ["FIVE", 55],
// ["TEN", 20],
// ["TWENTY", 60],
// ["ONE HUNDRED", 100]]

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]]);
``````

#2

Stick the following console.log statement inside your howMuchInDrawer function just before the return statement.

``````console.log(currentDrawerTotal);
``````

I think you will be surprised what you are returning.

#3

Thanks - I’ll try that and see what’s happening

#4

Yikes! OMG - i can’t believe that! I’m going to take a break and then get back at it soon. Thanks for the help!

#5

I have solved this algorithm! Thank you for the tips!