Cash Register : Javascript math tilting me again

Cash Register : Javascript math tilting me again
0

#1

so i was working the challenge, at the end everything was working like charm but the last test gave me a really weird result, i had 0.5000000000000002 in the penny array, i knew it was a problem with the JS float numbers since i crossed this before.
i used the VisualStudio Code debugger to track variables and i found the the error happened in the while loop when subtracting 0.01 from 0.47.

0.47 - 0.01 = 0.45999999999999996

i could easily handle this in my own file using math.js library but i don’t know any way to import this in the FCC challenge editor.

any suggestions ?

Your code so far


function checkCashRegister(price, cash, cid) {
  let cashArr = cid.map(e => e[1]),
      converted = [0.01, 0.05, 0.1, 0.25, 1, 5, 10, 20, 100],
      change = cash - price,
      // result = [0, 0, 0, 0, 0, 0, 0, 0, 0];
      result = [["PENNY", 0],  ["NICKEL", 0],  ["DIME", 0],  ["QUARTER", 0],  ["ONE", 0],  ["FIVE", 0],  ["TEN", 0],  ["TWENTY", 0],  ["ONE HUNDRED", 0]];
  for (let i = cashArr.length - 1; i >= 0; i--) {
    while(cashArr[i] > 0) {
      if (change - converted[i] >= 0) {
        result[i][1] += converted[i];
        change -= converted[i];
        cashArr[i] -= converted[i];
        if (change < 0.01 && change != 0) {
          change = 0.01;
        }
      } else { break; }
    }
    if (change === 0) {break}
  }
  result = result.filter(e => e[1] !== 0).reverse();
  if (change !== 0) {
    return {status: "INSUFFICIENT_FUNDS", change: []};
  }
  else if (change === 0 && cashArr.filter(Boolean) === []) {
    return {status: "CLOSED", change: result};
  } else {
    return {status: "OPEN", change: result};
  }
}

// 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]]

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]]));

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36.

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


#2

You could convert all the currency numbers to cents, by multiplying them by 100 (1.01 would become 101) and then perform all your calculations on them before finally converting them back to their decimal version (by dividing by 100). This will prevent the round issues inherent in most programming languages.


#3

thank you so much
i already passed the project using this function to perform calculation on float numbers

function strip(num) {
    return Number((parseFloat(num).toPrecision(12)));
}

not sure if it is the best way but it worked at least :slight_smile: i tried also the way you showed me and it was way easier.
thanks again