Challenge: JavaScript Algorithms and Data Structures Projects - Cash Register
Link to the challenge:
So, I want to know why when I return the change left to be count its return 0.009999999999999691 and not plain 0 as it should be. I need it to be 0 so the rest of my solution works. I did try it whit .toFixed() and .round() to make it 0, but “change === 0” isn’t true.
function checkCashRegister(price, cash, cid) {
let change = cash - price
//Stores the change in the exact amount of currrecy
let changeArr = [["PENNY", 0], ["NICKEL", 0], ["DIME", 0], ["QUARTER", 0], ["ONE", 0], ["FIVE", 0], ["TEN", 0], ["TWENTY", 0], ["ONE HUNDRED", 0]]
//Function where I pass the value of the currency and the position where said currency is stored and goes
const changeFunc = (num, index) => {
//If the change left is greater than the value of the currency and there is money left from the drawer
while(change >= num && cid[index][1] >= num){
//Add the change to its position in the array, substrac it from the drawer and from the change left
changeArr[index][1] += num
cid[index][1] -= num
change -= num
}
}
changeFunc(100, 8)
changeFunc(20, 7)
changeFunc(10, 6)
changeFunc(5, 5)
changeFunc(1, 4)
changeFunc(0.25, 3)
changeFunc(0.1, 2)
changeFunc(0.05, 1)
changeFunc(0.01, 0)
//The change left to be count. It should be 0, not 0.009999999999999691
return change
/* Rest of the code
if(change !== 0) {
return {
status: "INSUFFICIENT_FUNDS",
change: []
}
}
if(cid.every(elem => elem[1] === 0)){
return {
status: "CLOSED",
change: changeArr
}
}
if(!cid.every(elem => elem[1] === 0)){
return {
status: "OPEN",
change: changeArr.filter(elem => elem[1] > 0)
.sort((a, b) => a[1] - b[1])
.reverse()
}
}
*/
}
console.log(checkCashRegister(19.5, 20, [["PENNY", 0.5], ["NICKEL", 0], ["DIME", 0], ["QUARTER", 0], ["ONE", 0], ["FIVE", 0], ["TEN", 0], ["TWENTY", 0], ["ONE HUNDRED", 0]]))