# Cash Register - Get the right change

Really, Im stuck at how to get the cash register to qualify and return the right amount of currency. Been at it for a couple days now. I looked at a few of the other references but something isn’t clicking. Any help you can lend is much appreciated.

function checkCashRegister(price, cash, cid) {

var change = parseFloat(cash - price).toFixed(2);
console.log('Change:' + change)

//need to write code to check total CID to see if its sufficient to provide change

//calculate sum of the cash in drawer
let sumofCid = 0
for(let i = 0; i < cid.length; i++){
sumofCid += cid[i][1]
//return sumofCid.toFixed(2)
}
console.log('sum of cid: ' + sumofCid.toFixed(2))

//////////////////////////////////////

//use while to distribute change. Start at larger denomination and work your way down to smaller.
//

let denom = [
{ name: "ONE HUNDRED", val: 100.0 },
{ name: "TWENTY", val: 20.0 },
{ name: "TEN", val: 10.0 },
{ name: "FIVE", val: 5.0 },
{ name: "ONE", val: 1.0 },
{ name: "QUARTER", val: 0.25 },
{ name: "DIME", val: 0.1 },
{ name: "NICKEL", val: 0.05 },
{ name: "PENNY", val: 0.01 }
];

let currencyArr = []
let currencyArrTotal = 0

//if conditions are met this pushes object from denom array to changePayoutArr

while(change >= currencyArrTotal){

for(let i = 0; i < denom.length; i++){
if(change > denom[i]["val"])
currencyArr.push(denom[i])
//return currencyArr
}
console.log("Currency Arr " + JSON.stringify(currencyArr))

/// this loops through the currency array and give the sum of  values of the objects.
for(let k = 0; k < currencyArr.length; k++){
currencyArrTotal += currencyArr[k]["val"]
}
// console.log('payout array total: ' + currencyArrTotal)
}

////////////////////////////////////////////

let noFunds = {
status: "INSUFFICIENT_FUNDS",
change: []
}

let equalFunds= {
status: "CLOSED",
change: [cid]
}

let goodFunds = {
status: "OPEN",
change: currencyArr
}

//Final Conditions for output
if(sumofCid < change){ console.log(noFunds); return noFunds}
else if(sumofCid == change){console.log(equalFunds);return equalFunds}
else if(sumofCid > change){console.log(goodFunds); return goodFunds}
}

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