JavaScript Cash Register Exercise - Extremely long decimals?

Tell us what’s happening:
Hi all, I’ve been riddling myself with this challenge for going on a week now (unfortunately), and I can’t seem to gain much ground. I believe I’m headed in the right direction, but am at a point where I can’t figure out what is happening with my code exactly. Below’s code has to deal with whole numbers for the costs, and cash provided. So with 40 - 12, your expected change is 28, and the code returns. ["TWENTY",20,"FIVE",5,"ONE",1,"ONE",1,"ONE",1].

Which is the result I want. However, when the digits are decimals like, the cost is 5.46, and the cash provided is 10, and I console.log(); the subtraction, I get 4.54 3.54 2.54 1.54 0.54 0.29000000000000004 0.040000000000000036 0.030000000000000034 0.02000000000000003 0.010000000000000031

Which shows me that it begins subtracting the whole numbers, (4.54 - 1 = 3.54, 3.54 - 1 = 2.54, etc.) but then when it gets down to the coin values, it starts branching into decimals that are 10+ decimal places long. Any clue as to why this is happening? I know this is a long question, but I’ve been rattling my brain trying to understand this problem, and I am real eager to solve it on my own. Thank you!

Your code so far


function checkCashRegister(price, cash, cid) {
    let currency = {
        "ONE HUNDRED" : 100.00,
        "TWENTY" : 20.00,
        "TEN" : 10.00,
        "FIVE" : 5.00,
        "ONE" : 1.00,
        "QUARTER" : 0.25,
        "DIME" : 0.10,
        "NICKEL" : 0.05,
        "PENNY" : 0.01,
    };

    cid.reverse();
    let changeFromTransaction = cash - price;
    let cashInDrawer = 0;
    let changeDue = [];


    for (let i = 0; i < cid.length; i++){
      cashInDrawer += cid[i][1];
      
        for (let x in currency){
            if (cid[i][1] && currency[x] <= changeFromTransaction && cid[i][0] === x){
                while(currency[x] <= changeFromTransaction){
                changeFromTransaction -= currency[x]     
                changeDue.push(x);
                changeDue.push(currency[x]);
                }
                if (changeFromTransaction !== 0){
                    continue;
                }
                    return changeDue;
        }
    }
   }
}
console.log(JSON.stringify(checkCashRegister(12, 40, [["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.103 Safari/537.36.

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/javascript-algorithms-and-data-structures-projects/cash-register

This is a floating point error.
You will find that you don’t meet it if you change everything to whole number (0.01 becomes 1 etc multiplying by 100) and then converting back when you finished all maths

This video explain floating point errors well:

1 Like

This was incredibly useful… Thank you so much! :^)