HELP on Exact Change

Hi , Thanks for reading my questions.

I try to work out Exact Change and all examples pass/work except one example .

Pls check as below.

function checkCashRegister(price, cash, cid) {
  var change = [];
  var length = cid.length;
  // Here is your change, ma'am.
  var sum = 0;
  var funds = getFunds();
  var minus = cash - price;
  var units = {"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};
  function getFunds(){
    for(var i=0;i<length;i++){
      sum += cid[i][1];
    return sum;
  function countChange(index){
     var currentUnit = units[cid[index][0]];
     var count = cid[index][1];
     while(minus>=currentUnit && count>=currentUnit){
          minus-= currentUnit;count-= currentUnit;

  if(minus===0 || minus===0.50&&funds===0.50){return "Closed";}
  if(minus>funds){return "Insufficient Funds";}
      for(var i = length-1;i>=0;i--){
        if(minus===0){return change;}
   if(minus>0){return "Insufficient Funds";}

checkCashRegister(3.26, 100.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]]);

All other examples work/pass except below example:

checkCashRegister(3.26, 100.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]]);

Its result like ::

[["TWENTY", 60.00], ["TEN", 20.00], ["FIVE", 15.00], ["ONE", 1.00], ["QUARTER", 0.50], **["DIME", 0.20000000000018], ["PENNY", 0.03000000000027]]**

Really dont know why the last two elements will perform like as above…

Pls share your opinion on my work-out and point out the root cause.

It looks like you are encountering the problem with floating point math in javascript. I am sure others can speak to it much better. When I was working on this challenge, I remember going through the forums and finding a thread that contained a video about how math can be problematic in some languages and especially javascript.

I have seen a few different ways that people solved this issue, but you will need to do some rounding or other operation to make sure that you are returning just two decimal points for your change values. I hope this helps point you in the right direction. Doing a search on these forums should yield a good number of results for this challenge without giving the solution away.

1 Like

Do this instead
Var x = cid[index][1]-count;
x = Math.round(x * 100)/100;
This will make everything to two decimal places.
Then proceed here

change.push(cid[index][0], x);

Tip: Please comment your code, it will be easier to understand. I knew what the problem was but I had a hard time finding the line with the code.


Thanks for reply and I have realized the issue cause and learn a lesson on float operation.

1 Like

Hi , ed-kahara

Thanks for you time and list the solution to me.

It works and I have passed the challenge, and also I learnt a lesson from Math.round() and how to ask a question better and more efficiently.

Many thanks and best wishes.

Cherry. :slight_smile:


Cherry, I am really glad you realized what the issue was and learned something from it! It was the same for me on this lesson! Keep it up!

Never mind. I got it.