What am I missing? Exact Change Challenge - Returns correct values but does not pass

First time FCC poster. I apologize for my ugly code (probably with some unnecessary steps that I can’t find how to do without), but this my solution for the Exact Change algorithm challenge. After running the code with the assigned values for the test which failed, I saw that my answer contained the correct values, but the order for the array items in the requested value were reversed. I’m just wondering if the order of the array items is the reason why my code failed that test.

Here’s the test result and the requested answer:


The other tests which did pass had ascending value arrays. In addition, my lack of decimal points for full dollar values passed on the other tests (I thought maybe the decimals were the issue).

Here is my code:

function checkCashRegister(price, cash, cid) {
  var worth = [10000, 2000, 1000, 500, 100, 25, 10, 5, 1];
//Taking all cash-in-drawer values * 100 to avoid floating point precision issues
  for (i=0;i<cid.length;i++){
    cid[i][1] *= 100;
  var changeDue = (cash - price) * 100;
//Placeholder for change given back to customer at end
  var cashDue = [["PENNY", 0.00], ["NICKEL", 0.00], ["DIME", 0.00], ["QUARTER", 0.00], ["ONE", 0.00], ["FIVE", 0.00], ["TEN", 0.00], ["TWENTY", 0.00], ["ONE HUNDRED", 0.00]];
//Assignment of change
  for (i=0;i<worth.length;i++){
    if(changeDue >= worth[i] && worth[i] <= cid[8-i][1]){
      changeDue -= worth[i];
      cid[8-i][1] -= worth[i];
      cashDue[8-i][1] += (worth[i]);
//Return values to float point for cashier
  for (i=0;i<cid.length;i++){
    cashDue[i][1] /= 100;
//Checking if there is money still in the till  
  var sum = 0;
  function totalCash(){
      sum += cid[i][1];
    return sum;
//Final Conditions    
  if (changeDue > 0){
    return "Insufficient Funds";
  } else if (totalCash() === 0 && changeDue === 0){
    return "Closed";
  } else {
    var filterCash = [];
    for (i = 0;i<cashDue.length;i++){
      if (cashDue[i][1] !== 0){
    return filterCash;

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

Am I missing something here? Any help would be greatly appreciated.

The challenge instructions do say the following:

Otherwise, return change in coin and bills, sorted in highest to lowest order.

so the order is important.

1 Like

That’s what I missed! I changed the order of my array and passed! Thank-you so much for noticing my issue! :smile: