FCC progress halted due to "Exact Change" since Nov, 16

FCC progress halted due to "Exact Change" since Nov, 16
0

#1

After trying for so long to solve Exact Change problem, I am stuck with this test

checkCashRegister(19.50, 20.00, [["PENNY", 0.01], ["NICKEL", 0], ["DIME", 0], ["QUARTER", 0.50], ["ONE", 1.00], ["FIVE", 0], ["TEN", 0], ["TWENTY", 0], ["ONE HUNDRED", 0]]);

Can someone please tell me what I am not doing right?

Thanks
the full solution is below:

function checkCashRegister(price, cash, cid) {
  var change = cash - price;
  var totalcid = totalCid(cid);
  // console.log(change);
  var output = [];
  if(totalcid < change){
    return "Insufficient Funds";
  }
  if(change === totalcid){
    return "Closed";
  }
  
  for(var i = cid.length - 1; i >= 0; i--){
    
    var currencyUnit = interpretCurrency(cid[i][0]);
    
    // var remainder = change;
    var buffer = cid[i];
    // console.log(buffer);
    // console.log("change " +change);
    // console.log("currencyUnit " +currencyUnit);
    if(currencyUnit <= change){
      // console.log("change " +change);
      // console.log("currencyUnit " +currencyUnit);
      var returnUnits = 0;
      while((change / currencyUnit >= 1) && (cid[i][1] > 0)){
        change -= currencyUnit;
        change = change.toFixed(2);
        cid[i][1] -= currencyUnit;
        returnUnits++;
      }
      
      buffer[1] = returnUnits * currencyUnit;
      
      output.push(buffer);
    }
    
    
  }
  
  return output;
}


checkCashRegister(19.50, 20.00, [["PENNY", 0.01], ["NICKEL", 0], ["DIME", 0], ["QUARTER", 0.50], ["ONE", 1.00], ["FIVE", 0], ["TEN", 0], ["TWENTY", 0], ["ONE HUNDRED", 0]]);


function totalCid(arr){
  var total = 0;
  for(var i = 0; i < arr.length; i++){
    total += arr[i][1];
  }
  
  return total;
}


function interpretCurrency(str){
  var result = 0;
  switch(str) {
    case "PENNY":
      result = 0.01;
      break;
    case "NICKEL":
      result = 0.05;
      break;
    case "DIME":
      result = 0.10;
      break;
    case "QUARTER":
      result = 0.25;
      break;
    case "ONE":
      result = 1;
      break;
    case "FIVE":
      result = 5;
      break;
    case "TEN":
      result = 10;
      break;
    case "TWENTY":
      result = 20;
      break;
    case "ONE HUNDRED":
      result = 100;
      break;
    default:
      result = 0;
  }
  
  return result;
  
}

#2

Here’s an idea
Get rid of the switch statement and put an array of currency names and their values that’s just like the parameter ie
[[‘ONEHUNDRED’, 100],…]
Then reverse the array with the change left so now you have
[[‘ONE HUNDRED’, 0],…]

Then do what you have been doing, but modify it to deal with two arrays now.
I find the switch statement very weird, maybe changing it up will make it easier since you’ll just be going through an array of currency values and if change is smaller than the currency value, the program will simply move through the array constantly until it finds an appropriate one.
When it finds an appropriate denomination, it creates an array with that denomination name and sets value to zero eg in this case [‘QUARTER’, 0] and when you subtract 25 cents from the cash till, you add 25 cents to the new array so it becomes [‘QUARTER’, 0.25] and so on.

I hope this makes sense. This challenge was tough for me too, I’m suggesting using arrays because I find it easier to understand.


#3

Watch THIS video -
Stephen Mayeux is one awesome cat …


#4

You can make your code pass by making the same check you do at the beginning (where you check for “Insufficient Funds”) before returning.


#5

Ya, thanks, but I wanted to get the solution on my own…:slight_smile:


#6

just a reminder -