[Javascript Project] Cash Register

Tell us what’s happening:
I’m currently trying to finish this Cash Register Project, and I am a bit stuck on the third test. I know I cannot pass the 5th test, because my code does not yet handle it.
However, I don’t understand the result I get when trying the 3rd test : I get this :

“TWENTY,80,TEN,10,FIVE,5,ONE,1,QUARTER,0.5,DIME,0.2,PENNY,0.04” instead of
“[[“TWENTY”, 60], [“TEN”, 20], [“FIVE”, 15], [“ONE”, 1], [“QUARTER”, 0.5], [“DIME”, 0.2], [“PENNY”, 0.04]]”

I don’t understand what I did wrong here :frowning:
If anyone could point me in the right direction, it would be greatly appreciated ! Thanks a lot :slight_smile:

Your code so far

function checkCashRegister(price, cash, cid) {
  var change = {status: ' ', change: []};
  let changeDue = (cash-price)*100;
  const unitsName = ["ONE HUNDRED","TWENTY","TEN","FIVE","ONE","QUARTER","DIME","NICKEL","PENNY"];
  const unitsValue = [10000,2000,1000,500,100,25,10,5,1];
  var quantity, amount;
  let sumCid = (cid.reduce((a, b) => a + b[1], 0))*100; 
  let changeGiven = getExactChange(changeDue);

  function getExactChange(changeDue) {
    var changeTogive = [];
    for (let i = 0; i < unitsValue.length; i++) {
    quantity = Math.floor(changeDue / unitsValue[i]);
      if (quantity > 0) {
        changeTogive.push(unitsName[i]);
        amount = (quantity * unitsValue[i])/100;
        changeDue = changeDue % unitsValue[i]
        changeTogive.push(amount);
        
      }
    }
    return changeTogive; 
  }

  //final statement
  if (changeDue === sumCid) {
    change.status = "CLOSED";
    change.change = cid;
  } else if (sumCid < changeDue /*or not enough coins*/) {
    change.status = "INSUFFICIENT_FUNDS";
    change.change = [];
  } else {
    change.status = "OPEN";
    change.change.push(changeGiven);
  }

  console.log(change.status);
  console.log(changeGiven);
  return change;
}

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

Link to the challenge:

Hi there!

You need to take the available amount of each denomination and max the return change value based on that number. If the drawer only has $60 worth of twenty dollar bills, you can’t return $80 worth of twenty dollar bills. You have to return $60 of twenty dollar bills and then make up the difference in smaller bills where available.
-J

1 Like

Hi Jesse !

Thank you so much for your help !! It was so obvious I failed to see it …

Have a great day :slight_smile:

Audrey

1 Like

No problem!
As an exercise for myself in being able to interpret other people’s code, I went ahead and implemented the change. I’m going to drop my code in a spoiler below just in case you care to see it. It obviously isn’t the only way to accomplish it (probably not even the best) and it still returns a fail state even though it logs correctly but sometimes it helps seeing other people’s logic when you’re stuck.
-J

  let changeGiven = getExactChange(changeDue, cid);

  function getExactChange(changeDue, arr) {
    var changeTogive = [];
    var arr1 = arr.reverse();
    for (let i = 0; i < unitsValue.length; i++) {
      var max = arr1[i][1] / unitsValue[i] * 100;
      quantity = Math.floor(changeDue / unitsValue[i]);
      if (quantity > max) {
        quantity = max;
      }
      if (quantity > 0) {
        changeTogive.push(unitsName[i]);
        amount = (quantity * unitsValue[i])/100;
        changeDue = changeDue - amount * 100
        changeTogive.push(amount);
      }
    }
    return changeTogive; 
  }
1 Like

Wow Thank you Jesse ! That’s really helpful ! :smiley:
I think it returns a fail state because I push into a simple array , not a 2D array, but I’m not sure. Anyway, it’s great to see your improvement to my code !
I feel I am very close but still …

EDIT : Ok, I think I know why it fails : I believe my code is not updating the drawer after I use the previous bills. I have to figure out how to do that now. Thanks again for your help !

EDIT 2: I fixed it :slight_smile: And now Test 3 is OK !! Here is my code in case someone else is stuck like I was :

let changeGiven = getExactChange(changeDue, cid);

function getExactChange (changeDue,arr) {
      var quantity, amount; 
      var changeToGive = []; // result of the function that is going to be pushed into res.change 
      var arr1 = arr.reverse(); // change the order of cid to start at ONE HUNDRED
      // loop through all units values to substract max amount until there is no change left
      for (let i = 0; i < unitsValue.length; i++) { 
        var max = (arr1[i][1] * 100 ) / unitsValue[i]; // max available amount in drawer
        quantity = Math.floor(changeDue / unitsValue[i]) // number of coins/bills needed for change
        //console.log(quantity);
        if (quantity > 0) {
          if (quantity > max) { quantity = max;}
          amount = (quantity * unitsValue[i]) / 100;
          console.log(amount);
          changeToGive.push([unitsName[i],amount]);
          changeDue = changeDue - amount * 100;
        }
                  
      }
      return changeToGive;
    }