Cash Register TestCase Not Complete even answer is correct

Tell us what’s happening:
Hi, I have written some code to finish the Cash Register Task, but with the code below i can not pass the Test Case 3 even the printing result is same as the answer.

Also i am curious that why 96.74 - 60.00 is = 36.37999
Anyone knows? Thanks

Your code so far


function checkCashRegister(price, cash, cid) {
    var change= [];

    const unit = {
        "PENNY": 0.01,
        "NICKEL": 0.05,
        "Dime": 0.1,
        "QUARTER": 0.25,
        "ONE": 1,
        "FIVE": 5,
        "TEN": 10,
        "TWENTY": 20,
        "ONE HUNDRED": 100
    };
    var index = cid.length;
    let change_value = cash - price;
    var status = "emtpy";
    for (let i = index - 1; i >= 0; i--) {
        let CurrencyUnit = cid[i][0];
        let cash_in_drawer = 0;
        if (cid[i][1] != 0 && unit[CurrencyUnit] < change_value) {
            let token_cash;
            if(change_value > cid[i][1]){
                token_cash = cid[i][1];
            }else{
                token_cash = unit[CurrencyUnit] * Math.floor(change_value / unit[CurrencyUnit]);
            }       
            cash_in_drawer += cid[i][1];
            cash_in_drawer -= token_cash;
            change.push([cid[i][0], token_cash]);
            change_value = change_value -  token_cash;
            change_value = Math.round(change_value * 100) / 100;

            if(change_value == 0 && cash_in_drawer != 0){
                status = "OPEN";
            }else{
              status = "CLOSED";
              change = cid
            }
        }
    }
    if(change_value != 0){
        status = "INSUFFICIENT_FUNDS";
        change = [];
    }

    // Here is your change, ma'am.
    return {
        "status": status,
        "change": change
    };
}

// 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/73.0.3683.86 Safari/537.36.

Link to the challenge:
For security reasons, you can’t include links in your posts yet. Use the freeCodeCamp forum a bit more to unlock this ability. :smiley:

All calculations are done in binary, so sometimes decimals become periodic numbers (for example try console.log(0.1 + 0.2 === 0.3), it will be false), to avoid those floating point errors you could bring everything to units (multiply by 100), in this way you are working in cents - and then later you convert back before returning the value

A video to explain floating point error:

Can’t check the other issue right now, but to this I could answer

1 Like

You also have a typo here, where everything else is all capital letters
So you don’t have a unit["DIME"] (as unit[CurrencyUnit]) and when you use this in your code, the value is undefined

1 Like

Self Solved
It just a silly mistake there :smiley:
Just need to put the checking out of the For Loop

if(change_value == 0 && cash_in_drawer != 0){
        status = "OPEN";
    }else{
      status = "CLOSED";
      change = cid
    }
1 Like