Cash Register - truncated numbers

Cash Register - truncated numbers
0.0 0

#1

Tell us what’s happening:

I’m so angry at this language right now. I’ve never even asked for help in the forum before, but I’m kind of hopeless of making this work. I haven’t looked at other people’s code because I wanted to be able to do this on my own, so maybe this is not even the best approach to the problem.
But anyway, for some reason the code keeps truncating my numbers (as in 50-20 = 29,99999998). First it was making most of the tests fail, but I was able to fix it by using
currentChange.toFixed(2). Now only the last test fails, again because it somehow turns 0.04 - 0.01 into 0.29999999998 or something like that. The last test is this one:
(checkCashRegister(19.5, 20, [["PENNY", 0.5], ["NICKEL", 0], ["DIME", 0], ["QUARTER", 0], ["ONE", 0], ["FIVE", 0], ["TEN", 0], ["TWENTY", 0], ["ONE HUNDRED", 0]])).
I’ve tried adding toFixed(2) in other places of the code, but then it breaks it all and the tests that were passing don’t work anymore. I really don’t know what to do to fix it :frowning: can you guys help me? Should I just use integers for everything? Is there no way to make float calculations precisely in this god forsaken language? Thank you in advance.

Your code so far


let CURRENCY_VALUE = {
  "PENNY": 0.01,
  "NICKEL": 0.05,
  "DIME": 0.10,
  "QUARTER": 0.25,
  "ONE": 1.00,
  "FIVE": 5.00,
  "TEN": 10.00,
  "TWENTY": 20.00,
  "ONE HUNDRED": 100.00
}

function checkCashRegister(price, cash, cid) {
  let currentChange = (cash-price);
  let reversedCid = cid.reverse();
  let finalChange = [];

  reversedCid.forEach(availableCurrency => { //forEach[ONE HUNDRED, 100]...
    let change = 0;
    while (CURRENCY_VALUE[availableCurrency[0]] <= currentChange.toFixed(2) && availableCurrency[1] != 0) {
      change += CURRENCY_VALUE[availableCurrency[0]];
      availableCurrency[1] -= CURRENCY_VALUE[availableCurrency[0]];
      currentChange -= CURRENCY_VALUE[availableCurrency[0]];
    }
    if (change != 0) {
      finalChange.push([availableCurrency[0], change]);
    }
  })

  let totalValue = reversedCid.reduce((acc, curr) => {
    return acc + curr[1]
  }, 0); 
  // Here is your change, ma'am.
  return {status: currentChange>0?"INSUFFICIENT_FUNDS":totalValue===0?"CLOSED":"OPEN", change: currentChange>0?[]:[...finalChange]};
}

// 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: <code>Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36</code>.

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

#3

Another approach is to convert all the change into cents by multiplying everything by 100 before making any other calculations and then divide by 100 whenever you need to display the amount.


#4

Thank you. I was mad at javascript because I’ve never had that problem with C# or python, specially considering I’ve coded much more math-intensive functions in C#, but again, I’m fairly new to programming in general. I guess it was just me wanting to join the javascript hate team. Sorry if I sounded too angry. :slight_smile:


#5

Thank you, your approach sounds even easier. I’ll try both. :slight_smile:


#6

Python would have the same issue unless you are using a special library. Put the following in a file a run it with Python.

print(0.1 + 0.2) # displays 0.30000000000000004