JavaScript Algorithms Projects: Cash Register/nearly there but stuck

Hi Everybody!

I am doing this project and I can pass all the tests but this one.

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

I can understand why I am failing the test, that is because when I return my solution object, instead of returning one array with 7 nested arrays in it, I return only one array.

I have tried this solution (using brackets before pushing my answer) :

cashBack.push([myDrawer[j].name, money]);

In this case, I return one array with seven nested arrays, but I still fail the test. And worse, I fail a second test that was ok before.

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

So, here I am with this solution, and my head is fried. I work on this for so many hours, I can’t think clear anymore and I can’t see where is my error. So if anyone can have a look at it, I would be very glad for a little help!

function checkCashRegister(price, cash, cid){
  //Output change to give back to client
  var clientChange = cash - price;
  //console.log("client change = " + clientChange);
  
  //make the model of the object to return
  var moneyBack = {status: "not defined", change: []};
  
  //Output total amount of cash in drawer
  var total = [];
  for(var i = 0; i < cid.length; i++){
    total.push(cid[i][1]);
  }
  
  function getSum(count, num){
    return count + num;
  }
  
  var drawerTotal = total.reduce(getSum);
  //console.log("total cash in drawer = " + drawerTotal);
  
  //Logical operation to decide if we have enough money to give back change to client
  if(drawerTotal < clientChange){
    moneyBack.status = "INSUFFICIENT_FUNDS";
    return moneyBack;
  }
  
  else if(drawerTotal === clientChange){
    moneyBack.status = "CLOSED";
    moneyBack.change = cid;
    return moneyBack;
  }
  
  else {
    //create an drawer object with bills name, bills slot and cash available
    cid = cid.reverse();
    var myDrawer = [];
    var bills = [100, 20, 10, 5, 1, 0.25, 0.1, 0.05, 0.01];
    
    for(var i = 0; i < cid.length; i++){
      var drawer = {};
      drawer.name = cid[i][0];
      drawer.bills = bills[i];
      drawer.total = cid[i][1];
      myDrawer.push(drawer);
    }
    //console.log(myDrawer);
    
   //check what kind of bills you should give back to client, look from big bills to smaller one 
    var cashBack = [];
    for(var j = 0; j < myDrawer.length; j++){
      if(clientChange > 0 && clientChange >= myDrawer[j].bills && myDrawer[j].total > 0){
        var money = Math.floor(clientChange / myDrawer[j].bills) * myDrawer[j].bills;
        if(money > myDrawer[j].total){
          money = myDrawer[j].total;
        }
        cashBack.push(myDrawer[j].name, money);
        clientChange -= money;
        clientChange = +clientChange.toFixed(2);
        //console.log("client change is = " + clientChange);
        //console.log(cashBack);
      }
      if(clientChange === 0){
        break;
      }
      
    }//end of for loop
    moneyBack.change.push(cashBack);
    //console.log(cashBack);
    
      if(clientChange === 0){
        moneyBack.status = "OPEN";
      }
      else {
        moneyBack.status = "INSUFFICIENT_FUNDS";
        moneyBack.change = [];
      }      

  } //end of else statement logical operation
  
  
  // Here is your change, ma'am.
  console.log(moneyBack.status + moneyBack.change);
  return moneyBack;
  
}//end of function checkCashRegister


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

You are returning the array like this:

[["TWENTY", 60, "TEN", 20, "FIVE", 15, "ONE", 1, "QUARTER", 0.5, "DIME", 0.2, "PENNY", 0.04]]

But according to the requirements, you need wrap each change amount into its own array like this:

[["TWENTY", 60], ["TEN", 20], ["FIVE", 15], ["ONE", 1], ["QUARTER", 0.5], ["DIME", 0.2], ["PENNY", 0.04]]

The fix is pretty simple. Remove the following line:

moneyBack.change.push(cashBack);

And change this:

cashBack.push(myDrawer[j].name, money);

to this:

moneyBack.change.push([myDrawer[j].name, money]);
1 Like

Wow! Fantastic. Thank you so much for your help. I was banging my head to the wall not being able to see how to fix it. I am very gratefull.

1 Like