Get stuck Cash Register

Get stuck Cash Register
0

#1

Hello campers,please help ,this challenge made me so nervous I could not figure it out .

function checkCashRegister(price, cash, cid) {
let obj1={status: "INSUFFICIENT_FUNDS", change: []};
let obj2= {status: "CLOSED", change: []};
let obj3={status: "OPEN", change: []};
let reversed=cid.reverse();
let arr=[100, 20, 10, 5, 1, 0.25, 0.1, 0.05, 0.01];
let currency;
let change=cash-price;
let arrTwo=[];
let inMyRegister;
let len;
  // Here is your change, ma'am.
 inMyRegister=reversed.reduce(function(acc,item1){
            return acc+item1[1];
        },0);
 if(change>inMyRegister){
     return {status: "INSUFFICIENT_FUNDS", change: []};
 }
    function recurse(i){
         currency=currency-arr[i];
    }
    
for(let i=0;i<reversed.length;i++){
    currency=reversed[i][1];
    len=Math.ceil(reversed[i][1]/arr[i]);
    for(let j=0;j<=len;j++){
     if(change - currency>=0){
         arrTwo.push(currency);
        change=change-currency;
         break;
     }
        else if(currency==0){
            break;
        }
        else {
            recurse(i);
        }
    }
}
console.log(arrTwo);
}

checkCashRegister(3.26, 100, [["PENNY", 1.01], ["NICKEL", 2.05], ["DIME", 3.1], ["QUARTER", 4.25], ["ONE", 90], ["FIVE", 55], ["TEN", 20], ["TWENTY", 60], ["ONE HUNDRED", 100]]);

I know that I am pushing the currency in arrTwo but I would like just to see the result that I have and then I’ll fix that by returning objects.
if you check arrTwo in the console you’ll see thihs
[0, 60, 20, 15, 2, 0, -1.5265566588595902e-15, -1.429412144204889e-15, -7.528699885739343e-16]

I don’t know how to fix that ,please help


#2

that is simply due to javascript rounding errors. It is like how 10/3 isn’t exactly to 3.333333333333333333333333333333333333333333333333333333333333.

anyway to solve that issue, use Math.round() or Math.floor().


#3

the issue is in the change variable when I console in the first loop this is what it gives me
96.74
36.739999999999995
16.739999999999995
1.7399999999999949
0.7399999999999949
0.23999999999999488
0.0399999999999964
0.03999999999999783

the change is not accurate


#4

Floating point precision is a problem that we always have to work with when it comes to computers. My advice for this problem is to think of the currency as cents rather than dollars. In other words, internally to your solution multiply everything by 100 so you can work with integers.


Cash Register Project strange output
#5

This challenge took from me a lot of time untill I get frustrated , I thought I was not efficient, some thoughts push me to build up my confidence from time to time ,untill I get the solution ,thanks man that was so helpful ,thank you all .