Exact Change question about divide

Exact Change question about divide
0.0 0

#1

Tell us what’s happening:
Hello
I’m working on this algorithm
At first I’m trying to determine how much coins and banknotes are in the cach-in-drawer
for this purpose I divided the amount by the nominall
as example:
const penny = cid[0][1]/0.01;// here is ok

but when I divided this I got this
const nickel = cid[1][1]/0.05; // 40.9999999999999 not 41

how can I solve it and get 41?

Your code so far

function checkCashRegister(price, cash, cid) {
  
  const penny = cid[0][1]/0.01;
  const nickel = cid[1][1]/0.05;
  const dime = cid[2][1]/0.1;
  
  
  var change = cash - price;
  
  return penny ;
}

// Example cash-in-drawer array:
// [["PENNY", 1.01], if PENNY , 
// ["NICKEL", 2.05],
// ["DIME", 3.10],
// ["QUARTER", 4.25],
// ["ONE", 90.00],
// ["FIVE", 55.00],
// ["TEN", 20.00],
// ["TWENTY", 60.00],
// ["ONE HUNDRED", 100.00]]

checkCashRegister(19.50, 20.00, [["PENNY", 1.01], ["NICKEL", 2.05], ["DIME", 3.10], ["QUARTER", 4.25], ["ONE", 90.00], ["FIVE", 55.00], ["TEN", 20.00], ["TWENTY", 60.00], ["ONE HUNDRED", 100.00]]);

Your browser information:

Your Browser User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.139 Safari/537.36.

Link to the challenge:


#2

The problem you are seeing has to do with floating point precision (you may find this helpful if you are interested).

In this case, you can round your results off to 2 decimal places with Number.prototype.toFixed and it shouldn’t affect your results:

const nickel = (2.05/0.05);

nickel; // "41.00". Note that the output of `toFixed()` is a string.

I hope that helps! :smile: