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/