Right But Wrong - Cash Register

Right But Wrong - Cash Register
0

#1

My code is passing all tests except the very last one where the function should return “Closed” along with the cash-in-drawer array. When running in JS Bin, everything looks good and the output matches exactly the expected output shown in the FCC test. But, it’s not passing. Am I losing my mind?

Full code below,

Thanks!

function checkCashRegister(price,cash,cid){ 


var dollarsValueArray = [0.01, 0.05, 0.1, 0.25, 1, 5, 10, 20, 100];


function totalToNumberOfBills(value, index){
    
    return Math.round(value[1]/dollarsValueArray[index]);
  }
  
var startingNotesAvailable = cid.map(totalToNumberOfBills);
    
var change = cash - price;
  
      
function countCoinsNeeded(total,amt,index,array){
  
    var currentCoin = dollarsValueArray[index];
           
    var curCID = cid[index][0];
            
    if(total[0] < currentCoin) { 
      
      total[1].unshift([0,currentCoin]);
      
      return total;        
           
    } else {
             
    var coinsNeeded = Math.floor(Math.round(total[0]/currentCoin * 100)/100);
            
    if(coinsNeeded <= amt){
    
     total[1].unshift([coinsNeeded,currentCoin]);
      
     total[0] = Math.round(((total[0]*100) % (currentCoin * 100)))/100;
      
     return total; 
                           
    } else {
         
      total[1].unshift([amt,currentCoin]);
      
      total[0] = Math.round((total[0] - amt * currentCoin)*100)/100
      
      return total;        
    }
   }
}
   
  var billsNeededAndChange = startingNotesAvailable.reduceRight(countCoinsNeeded,[change,[]]);
      
    
  function adjustCash(acc,currVal,index,arr){
    
       
    var billUsed = billsNeededAndChange[1][index][0];
       
    return acc + currVal - billUsed;
  }
  
  var sumFinalNotesAvailable = startingNotesAvailable.reduce(adjustCash,0);
  
    
  function addChangeLabels(cur,index){
    
    return [cid[index][0],cur[0]*cur[1]]; //returns note name (i.e. penny) and change x note value (2 * 0.0.1)
  }
  
  function reverseArray(cur,index,array){
    
    return array[array.length - index -1];
  }
  
  var changeWithLabels = billsNeededAndChange[1].map(addChangeLabels).map(reverseArray);
  
  var finalCID = billsNeededAndChange[1].map(addChangeLabels);
  
  var noZeroChangeNotes = changeWithLabels.filter(el => el[1] !== 0);
  
    
  function checkChangeRemaining(){
    
    if(billsNeededAndChange[0] > 0){
   
    var noFunds = {status: "INSUFFICIENT_FUNDS", change: []};
    
    return noFunds;
  
  
    } else if(billsNeededAndChange[0] === 0){
      
       if(sumFinalNotesAvailable === 0){
         
         var closed = {status: "ClOSED", change: cid};
         
         return closed;         
         
       }
            
      return {status: "OPEN", change:noZeroChangeNotes};
    }
    
  }
    
  return checkChangeRemaining();

}

checkCashRegister(19.5, 20, [["PENNY", 0.5], ["NICKEL", 0], ["DIME", 0], ["QUARTER", 0], ["ONE", 0], ["FIVE", 0], ["TEN", 0], ["TWENTY", 0], ["ONE HUNDRED", 0]]);

#2

You are using a lowercase l instead of uppercase…
Small fix…


#3

Jeez.

I thought I was in the Twilight Zone or something.

Thanks for catching that!