Cash register algorithm HELP!

Cash register algorithm HELP!
0

#1

Hi,

I have been stuck with this exercise for a while. One of the complications I have found, is that since JavaScript sometimes fails with the sum of float numbers, the result was always unexpected (for example 1.01 + 2.05 is not 3.06). I searched for a solution online but my tests keep failing.

This is my code:

function checkCashRegister(price, cash, cid) {
  var toReturn = cash - price;
  var cashArray = [0.01, 0.05, 0.1, 0.25, 1, 5, 10, 20, 100];
  var counter = 0;
  var total = 0;
  var result = {
    status: "",
    change: [["ONE HUNDRED", 0], ["TWENTY", 0], ["TEN", 0], ["FIVE", 0], ["DOLLAR", 0], ["QUARTER", 0], ["DIME", 0], ["NICKEL", 0], ["PENNY", 0]]
  }
  for(var j = 0; j < cid.length; j++){
    total = (total * 10 + cid[j][1] * 10) / 10;
    if (j == cid.length - 1){
      total = total * 100;
    var a = Math.round(total);
    total = a / 100;
    }
  }
  if(total < toReturn){
    result.status = "INSUFFICIENT_FUNDS";
    result.change = [];
    return result;
  } else if(total == toReturn){
    result.status = "CLOSED";
  } else {
    result.status = "OPEN";
  for(var i = cid.length-1; i >= 0; i--){
    if (toReturn - cashArray[i] >= 0){
      while(cid[i][1] > 0 && toReturn - cashArray[i] >= 0){
        cid[i][1] = cid[i][1] - cashArray[i];
        result.change[counter][1] += cashArray[i];
        toReturn = toReturn - cashArray[i];
        var b = Math.round(toReturn * 100);
        toReturn = b / 100;
      }
    }
    counter++
  }
  }
  var filtered = result.change.filter(function(x){
    return x[1] > 0;
  })
  result.change = filtered;
  return result;
}


checkCashRegister(19.5, 20, [["PENNY", 1.01], ["NICKEL", 2.05], ["DIME", 3.1], ["QUARTER", 4.25], ["ONE", 90], ["FIVE", 55], ["TEN", 20], ["TWENTY", 60], ["ONE HUNDRED", 100]]);

I appreciate any help provided.

edit:

this last condition goes against all the other conditions, I mean :
checkCashRegister(19.5, 20, [[“PENNY”, 0.5], [“NICKEL”, 0], [“DIME”, 0], [“QUARTER”, 0], [“ONE”, 0], [“FIVE”, 0], [“TEN”, 0], [“TWENTY”, 0], [“ONE HUNDRED”, 0]]) should return {status: “CLOSED”, change: [[“PENNY”, 0.5], [“NICKEL”, 0], [“DIME”, 0], [“QUARTER”, 0], [“ONE”, 0], [“FIVE”, 0], [“TEN”, 0], [“TWENTY”, 0], [“ONE HUNDRED”, 0]]}.

There are many “0” and they still appear even though it is supposed to return only the ones that are not 0, and in descending order, in this case, it requires to return in ascending order.


#2

Carefully reread the challenge requirements regarding the responses and you will find the answer to your most recent question.


#3

ok, I made a mistake in the requirements.

After re-reading the requirements and correcting the mistakes, this is my code, but ONE condition is still not being accepted, even though it returns what it requires.

function checkCashRegister(price, cash, cid) {
  var toReturn = cash - price;
  var cashArray = [0.01, 0.05, 0.1, 0.25, 1, 5, 10, 20, 100];
  var counter = 0;
  var total = 0;
  var result = {
    status: "",
    change: [["ONE HUNDRED", 0], ["TWENTY", 0], ["TEN", 0], ["FIVE", 0], ["DOLLAR", 0], ["QUARTER", 0], ["DIME", 0], ["NICKEL", 0], ["PENNY", 0]]
  }
  for(var j = 0; j < cid.length; j++){
    total = (total * 10 + cid[j][1] * 10) / 10;
    if (j == cid.length - 1){
      total = total * 100;
    var a = Math.round(total);
    total = a / 100;
    }
  }
  if(total < toReturn){
    result.status = "INSUFFICIENT_FUNDS";
    result.change = [];
    return result;
  } else if(total == toReturn){
    result.status = "CLOSED";
    result.change = cid;
    return result;
  } else {
    result.status = "OPEN";
  for(var i = cid.length-1; i >= 0; i--){
    if (toReturn - cashArray[i] >= 0){
      while(cid[i][1] > 0 && toReturn - cashArray[i] >= 0){
        cid[i][1] = cid[i][1] - cashArray[i];
        result.change[counter][1] += cashArray[i];
        toReturn = toReturn - cashArray[i];
        var b = Math.round(toReturn * 100);
        toReturn = b / 100;
      }
    }
    counter++
  }
  }
  if (toReturn != 0){
    result.status = "INSUFFICIENT_FUNDS";
    result.change = [];
    return result;
  }
  var filtered = result.change.filter(function(x){
    return x[1] > 0;
  });
  result.change = filtered;
  return result;
}

This condition is not being fulfilled;

checkCashRegister(3.26, 100, [[“PENNY”, 1.01], [“NICKEL”, 2.05], [“DIME”, 3.1], [“QUARTER”, 4.25], [“ONE”, 90], [“FIVE”, 55], [“TEN”, 20], [“TWENTY”, 60], [“ONE HUNDRED”, 100]]) should return {status: “OPEN”, change: [[“TWENTY”, 60], [“TEN”, 20], [“FIVE”, 15], [“ONE”, 1], [“QUARTER”, 0.5], [“DIME”, 0.2], [“PENNY”, 0.04]]}.