Exact Change Challenge, result formatting

Exact Change Challenge, result formatting
0

#1

The Issue:

I’ve resolved the ‘Exact Change’ challenge however I’m not able to pass tests 4 and 5.

The result is correct however the format required is float( 0.00 ). I’m able to output a “0.00” format however a string as output is rejected and numbers with trailing zeros are automatically trimmed as these have no use.

Additionally for some reason test 7 fails while my solution yields the exact same string as expected (“Insufficient Funds”).

The main focus seems to be the ability to come up with the right amounts, format is irrelevant as when used as number it doesn’t make any difference and if used for presentation a string gets the job done.

Am I missing something?

My Code

  {
    name:'ONE HUNDRED',
    denomination:100.00
  },
  {
    name:'TWENTY',
    denomination:20.00
  },
  {
    name:'TEN',
    denomination:10.00
  },
  {
    name:'FIVE',
    denomination:5.00
  },
  {
    name:'ONE',
    denomination:parseFloat(1.00)
  },
  {
    name:'QUARTER',
    denomination:0.25
  },
  {
    name:'DIME',
    denomination:0.10
  },
  {
    name:'NICKEL',
    denomination:0.05
  },
  {
    name:'PENNY',
    denomination:0.01
  }
];

function checkCashRegister(price, cash, cid) {

  var change = cash - price;

  let drawer = denominations.map(function(a){
    a.available = cid.reduce(function(c,b){
      if(b[0] == a.name){
        return b[1];
      }
      return c;
    },0);
    return a;
  });

  var drawerCash = drawer.reduce(function(c,a){
    c += a.available;
    return c;
  },0);

  if(drawerCash < change){
    return 'Insufficient Funds';
  }
  if(drawerCash == change){
    return 'Closed';
  }

  let changeComposition = drawer.reduce(function(c,a){

    if(!(change < a.denomination && change > a.available)){

      let ref = a.available < change ? a.available: change;
      
      let diff = Math.floor(Math.round(ref*100)/100/a.denomination);
      
      let denominationAmount = diff * a.denomination;
            
      change -=  denominationAmount;

        if(diff > 0){
          c.push([
            a.name,
            denominationAmount.toFixed(2)
          ]);
        }
      
    }
    return c;

  },[]);

  if(change > 0){
    return 'Insufficient Funds';
  }  

  return changeComposition;

}

// Example cash-in-drawer array:
// [["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]]

checkCashRegister(19.50, 20.00, [["PENNY", 0.01], ["NICKEL", 0], ["DIME", 0], ["QUARTER", 0], ["ONE", 1.00], ["FIVE", 0], ["TEN", 0], ["TWENTY", 0], ["ONE HUNDRED", 0]]);```
**Your browser information:**

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

**Link to the challenge:**
https://www.freecodecamp.org/challenges/exact-change

#2

Ran into the same issue as well


#3

If you want to post your current code that is not working, we can look at it and give you some pointers.