Please help with cash register project

Hi guys, I have been working on the cash register for the last couple of days, and I THINK I am getting closer to the solution. So far I have completed all the other projects without looking at solutions, and I REALLY don’t want to look at how to solve this one and I want to do it myself, but I think I am stuck and need a little help to get there. So far this is my code:

function checkCashRegister(price, cash, cid) {

  const valueTable = {
    "ONE HUNDRED": 100,
    "TWENTY": 20,
    "TEN": 10,
    "FIVE": 5,
    "ONE": 1,
    "QUARTER": 0.25,
    "DIME": 0.1,
    "NICKEL": 0.05,
    "PENNY": 0.01
  }
  
  var change = [];
  var changeDue = cash - price;
  // figure out total cid
  var totalDrawer = 0;
  for (let i = 0; i < cid.length; i++) {
    totalDrawer+= cid[i][1];
  }
 // if change > total cid 
 if (changeDue > totalDrawer) {
   return {status: "INSUFFICIENT_FUNDS", change: []}
 }  
  
  // loop through drawer to give change

  for (let i = cid.length - 1; i >= 0; i--) {
    if(changeDue >= valueTable[cid[i][0]]) {
      var temp = [];
      var changeGiven = 0;
      temp.push(cid[i][0])
      
      while(changeDue - valueTable[cid[i][0]] >= 0 && cid[i][1] !== 0) {
        cid[i][1] -= valueTable[cid[i][0]];
        changeDue -= valueTable[cid[i][0]];
        changeGiven += valueTable[cid[i][0]];
        
      }
      temp.push(changeGiven);
      change.push(temp)
    }
  
  }


// return status OPEN and change given if change has been given successfully
  if (changeDue == 0) {
  return {status: "OPEN", change: change}}

// if change wasn't given successfully return insufficient funds
  else return {status: "INSUFFICIENT_FUNDS", change: []}


  
}

I know this isn’t complete yet. I still haven’t handled the case where change is given successfully and is exactly the available change in the drawer, giving status: CLOSED as output.

But this passes a few tests already, and I noticed it fails a test that it SHOULD pass:

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

And the reason it fails this test is because change gets lost in the way when Javascript handles decimal numbers, and the changeDue variable never gets to 0 but there is always a very small number left. (I know this by doing a few console.log in strategic places).

Now my question is, before I keep working on this approach. Is it fixable? if it is, where do I look for solutions to this problem? Any help or hint that will help me move forward is highly appreciated!!

thank you!

You can manage without too many changes

I suggest this video to understand what’s going on, and there is also a suggestion on the approach to use hidden in there

Thank you @ilenia for the help!! the video was super helpful. I came back to the problem this morning and managed to solve it :smiley: :smiley: -

I took the hint and decided to multiply everything by 100 - I actually had to multiply by 10 twice, or it would give me the same error - work the algorithm and then divide everything by 100 at the end to return the expected values.

Here is the code that allowed me to solve the challenge and gain my second certificate here on freeCodeCamp - so exciting!!! - I would love if you or anyone else could give me some feedback.

function checkCashRegister(price, cash, cid) {
  // all number will be multiplied by 100 to avoid mistakes
  var change = [];;
  var changeDue = (cash - price) * 10 * 10;
  
  //loop through drawer to multiply values
  for (let i = 0; i < cid.length ; i++) {
    cid[i][1] = cid[i][1] * 10 * 10;
  }

  // create variable to store total money in drawer
  var totalDrawer = 0;
  for (let i = 0; i < cid.length; i++) {
    totalDrawer += cid[i][1];
  }
  
  // table to store value of each currency in cents
  const valueTable = {
    "ONE HUNDRED": 10000,
    "TWENTY": 2000,
    "TEN": 1000,
    "FIVE": 500,
    "ONE": 100,
    "QUARTER": 25,
    "DIME": 10,
    "NICKEL": 5,
    "PENNY": 1
  }

  // loop through drawer to give change

  for (let i = cid.length - 1; i >=0; i--) {
    var temp = [];
    var changeGiven = 0;
    var currentCurrency = valueTable[cid[i][0]]; // value of each currency
    var availableCurrency = cid[i][1]; // available money in that currency
    if (changeDue >= currentCurrency){
      if (availableCurrency > 0) {
        temp.push(cid[i][0]); // push that currency to temp
        while(availableCurrency > 0 && changeDue >= currentCurrency){ //keep giving change and taking it from changedue and the available currency until possible
          changeGiven += currentCurrency;
          availableCurrency -= currentCurrency;
          changeDue -= currentCurrency;
        }
        changeGiven /= 100; // divide change given by 100 before pushing it to temp
        temp.push(changeGiven);
        change.push(temp) //push the currency you used and their values divide by 100 (units of 1 instead of cents) to change array
      }
    }
  }

// loop through drawer again to reset to normal values (values/100)
for (let i = 0; i < cid.length ; i++) {
    cid[i][1] = cid[i][1] / 10 / 10;
  }

// return results according to instructions

  if (changeDue === 0 && totalDrawer / 100 === cash - price) {
    return {status: "CLOSED", change: cid};
  }
  
  else if (changeDue === 0) {
    return {status: "OPEN", change: change};
  }

  else {
    return {status: "INSUFFICIENT_FUNDS", change: []}
  }
}

Now I am going to look at other people’s solutions, but hopefully mine isn’t too bad!!

Thanks for you help!

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.