76.34 - 20 = 56.339999 !?!?!

Tell us what’s happening:
For some reason, in my loop i subtract moniesValue from changeValue
which is 76.74 - 20
yet it returns 56.7399999999 which causes errors

Why is this happening…
As you can see i even printed values to console, with the sum and even there it sums it wrong

Your code so far


function checkCashRegister(price, cash, cid) {

var change = {
  status: "",
  change: []
};

let changeValue = Math.abs(price - cash);
console.log(changeValue);


let moniesValue = [.01, .05, .1, .25, 1, 5, 10, 20, 100 ];
let moniesName = ["PENNY", "NICKEL", "DIME", "QUARTER", "ONE", "FIVE", "TEN", "TWENTY", "ONE HUNDRED"];
let moniesAmt = [0,0,0,0,0,0,0,0,0];

if(moniesAmt.reduce((sum, item) => {sum + item}) == price){
  change.change = cid;
  change.status = "CLOSED"
  return change;
}

//assign values
for(let x = 0; x < cid.length; x++){
  if(moniesName[x] == cid[x][0]){
    moniesAmt[x] = cid[x][1];
  }
}

let bool = false;
for(let x = moniesName.length - 1; x >= 0; x--){
  bool == false;
  //console.log(changeValue + " moniesAmt of x: " + moniesAmt[x])
  while(changeValue >= moniesValue[x] && moniesAmt[x] != 0 && moniesAmt[x] - moniesValue[x] >= 0 && changeValue != 0){
    console.log("changeValue: " + changeValue + " moniesValue[x]: " + moniesValue[x] + " sum: " + (changeValue - moniesValue[x]))
    moniesAmt[x] -= moniesValue[x];
    changeValue -= moniesValue[x];
    if(bool == false){
     // console.log("sssssssss")
      bool = true;
      change.change.unshift([moniesName[x], moniesValue[x]])
    }else {
      change.change[0][1] += moniesValue[x];
      //console.log(change.change)
    }
  }
}

console.log("pre insufficient check: " + changeValue)
//if there wasn't enough in register
if(changeValue != 0){
  change.change = []
  change.status = "INSUFFICIENT_FUNDS"
}else {
  change.status = "OPEN";
}

console.log(change)


return change;
}//

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



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

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:84.0) Gecko/20100101 Firefox/84.0.

Challenge: Cash Register

Link to the challenge:

This is due to how floating point numbers are stored in your computer. Rounding errors are inevitable with floating point numbers. I recommend instead keeping track of a integer number of cents in this project.

Computers cannot store floating point (decimal) numbers precisely.

1 Like

To add to what’s already been posted, the solution is to either use toFixed() or toPrecision() when you have to work with decimal numbers:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/toFixed
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_objects/Number/toPrecision

Or as already mentioned, in some cases it can make a lot of sense to keep integer counts of fractional values (i.e. pennies, nickels, dimes, quarters, etc).

2 Likes

I always forget about the existence of fixed precision numbers since I so rarely have use for them. As I understand it, in JavaScript fixed precision numbers in this problem would effectively keep track of the integer number of cents for you.

Edit: Eww, it converts to a string!?!

Some viewing on why floating points can troublesome.

Something to be aware of: toFixed and toPrecision return both return strings, not numbers.

1 Like