I am not done with this project, but I am stuck in one part of the project. I am having trouble just pushing the numbers that will make my total 0. For example if the change I need to give back is 5.50 my array should be [5,.50]. My numbers in my Array should not exceed the numbers inside myArr1. I might not be phrasing the question right but any insight on how to tackle this problem would be wonderful. Thank you!
function checkCashRegister(price, cash, cid) {
let myArr = [];
let myChange = 0
let change = cash-price;
let total = cash - price
let myArr2 = []
let myMon = [["PENNY", .01], ["NICKEL", .05], ["DIME",.1, ],["QUARTER", .25], ["ONE", 1],["FIVE", 5],["TEN", 10],[ "TWENTY", 20],["ONE HUNDRED", 100]]
let myArr1 = {"PENNY":1.01, "NICKEL": 2.05,"DIME":3.1,"QUARTER": 4.25, "ONE": 90,"FIVE": 55,"TEN": 20, "TWENTY": 60,"ONE HUNDRED": 100}
let myNum = {"PENNY":.01, "NICKEL": .05,"DIME":.1,"QUARTER": .25, "ONE": 1,"FIVE": 5,"TEN": 10, "TWENTY": 20,"ONE HUNDRED": 100}
for(let i =0; i <myMon.length; i++){
if(myMon[i][1] <= change){
myArr.unshift(myMon[i][1])
}
}
for(let i = 0; i<myArr.length; i++){
myArr2.push(total -= myArr[i])
}
return myArr2
}
console.log(checkCashRegister(70, 100, [["PENNY", 1.01], ["NICKEL", 2.05], ["DIME", 3.1], ["QUARTER", 4.25], ["ONE", 90], ["FIVE", 55], ["TEN", 20], ["TWENTY", 60], ["ONE HUNDRED", 100]]));
Don't work with fractions for currency. Javascript's Number type is binary-based floating point only, which means some decimal fractions aren't exactly represented. See below:
//working with fractions of Dollars
let change = 6.30
let dime = 0.10
let dimeCoins = change / dime //you won't get exactly 63 dimes
console.log(63, dimeCoins, 63 - dimeCoins)
//working with cents (no inexact rounding of fractional values)
change = 630
dime = 10
dimeCoins = change / dime
console.log(63, dimeCoins, 63 - dimeCoins)
As for your problem model, keep in mind that for every currency denomination, the cash register has a number of units with the corresponding individual value.
When picking change, the cashier always tries to exhaust the greatest bills before moving on to the lesser, next greatest ones.
You can solve this problem using no objects, two arrays and a for loop.