Cash Register-Increment currency value until it reaches a value in cash drawer

Cash Register-Increment currency value until it reaches a value in cash drawer
0

#1

Tell us what’s happening:
I’m no where near completing this exercise but I have managed to create an array of currency values and an array of available cash. My aim so far is to create an array that contains the correct cash denominations first.

I need help on how to increment a value in var currencyTable while (changeDue >= currencyTable[i]) up to the available total of that particular cash denomination in cashInDrawer[i].

I’ve modeled my code after the Roman Numeral exercise, pushing values as they pass the test but I’ve not managed to return an array with the correct cash denominations, only an array that contains the correct change but not with the cash available.

Also, during some part of the loop the variable changeDue turns into a repeating decimal and I don’t know why; I’ve managed a temporary workaround but I’d like to understand why this is occurring.
Your code so far


function checkCashRegister(price, cash, cid) {
  var changeDue = cash - price;//console.log(changeDue)
  var change = changeDue;
  var cashInDrawer = [];
  var currencyTable = [100, 20, 10, 5, 1, 0.25, 0.1, 0.05, 0.01];
  //console.log(currencyTable)
  for (let k = cid.length-1; k >= 0; k--){
    for (let j = cid[k].length-1; j > 0; j--){
      cashInDrawer.push(cid[k][j])
    }
  }//return cashInDrawer
  var total = [];
  for (let i = 0; i < currencyTable.length; i++){
  while (changeDue >= currencyTable[i]){
//I thought using Math.max() might let me increment currencyTable[i]+currencyTable[i] up to the cash available in cashInDrawer[i]
    var max = Math.max(currencyTable[i]+=currencyTable[i], cashInDrawer[i]%changeDue)
    total.push(max)
    changeDue-=max
  }
 }return total;
}
}

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

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.110 Safari/537.36.

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/javascript-algorithms-and-data-structures-projects/cash-register/


#2

Hi,

Just to make sure that I understand what your are building. The currencyTable is where you want to compute the change?

If so I think that you should use the same structure as cid given in the input and initialized to 0 (2D array)

var currencyTable = [["PENNY", 0], ["NICKEL", 0], ["DIME", 0], ["QUARTER", 0], ["ONE", 0], ["FIVE", 0], ["TEN", 0], ["TWENTY", 0], ["ONE HUNDRED", 0]];

In your outer loop index by k:

  • access the cid sublist at index k (example k=0 returns [“PENNY”, 1.01])
  • Extract the value in the sublist (1.01 in the above example)
  • Compute how much of this value you will use
  • access currencyTable k index value and replace the 0 with your result (using [k][1] for example)

Once you have looped on all the currency units, your currencyTable should have the desired answer.
Create an object based on which case you are in to return out of the function (not enough cash, just right, remaining cash in register)

Good luck!