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.

Thanks in advance.

Your code so far

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"])
 //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 = {
 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]]);

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_6) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/14.0.2 Safari/605.1.15.

Challenge: Cash Register

Link to the challenge:

Notice two things about result for the test case at the bottom of code. Change in result is looking slightly different comparing to the example result. Keep in mind this doesn’t mean way function keeps things internally is wrong, it’s just something to remember that it needs to be dealt with when result is returned. Another thing to notice is that somehow each coin is included twice in result, what effects in change total higher than it should by almost a half.

These small tips should give you some idea where to look closer in code.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.