Cash Register Returning Funds Issue

Tell us what’s happening:
Hey guys I could use some help with some issue about the Cash Register Project.

I’m struggling when trying to give the money back, in the last else if at the bottom of the code, you can see how the decimals mess up at some point doing the Math.

I’ll be very thankful if you could give some ideas or advices to complete this challenge. I’m stuck with this one.

Also, I know my code is not very efficient so, if you have an advice to improve I’d like to learn.

Your code so far


function checkCashRegister(price, cash, cid) {
  let currencyArr = [
    ["PENNY", 0.01],
    ["NICKEL", 0.05],
    ["DIME", 0.1],
    ["QUARTER", 0.25],
    ["ONE", 1],
    ["FIVE", 5],
    ["TEN", 10],
    ["TWENTY", 20], 
    ["ONE HUNDRED", 100]];
  let change = Math.abs(price - cash);
  let total = 0;
  let currency = [];
  let auxCurr = [];
  let acum = 0;
  let acumTemp = 0;
  let diff = 0;
  
  for(let i = 0; i < cid.length; i++) {
    currencyArr[i].push(cid[i][1]);
  }
  currencyArr.map(function(val, index) {
    if(val[1] < change && val[2] > 0) {
      auxCurr.push(val);
      total += auxCurr[index][2];
    }
  });
  currency = auxCurr;
  //currency.map((val, index) => val.splice(1, 1));
  total = Math.round(total * 100) / 100;
  console.log(total);
  console.log(auxCurr);
  
   if(total < change) {
     console.log("INSUFFICIENT_FUNDS");
     return {status: "INSUFFICIENT_FUNDS", change: []};
   }
   
   else if(total === change) {
     console.log("CLOSED");
     return {status: "CLOSED", change: cid}
   }
   
   else if(total > change) {
     for(let i = auxCurr.length - 1; i >= 0; i--) {
      while(acum < change && acumTemp < auxCurr[i][2]) {
        acum += auxCurr[i][1];
        acumTemp += auxCurr[i][1];
        console.log(acum)
      }
      if(acum > change) {
        acum -= auxCurr[i][1];
      }
      acumTemp = 0;
    }
    //return {status: "OPEN", change: currency}
   }
   
  // Here is your change, ma'am.
  return change;
}

// Example cash-in-drawer array:
// [["PENNY", 1.01],
// ["NICKEL", 2.05],
// ["DIME", 3.1],
// ["QUARTER", 4.25],
// ["ONE", 90],
// ["FIVE", 55],
// ["TEN", 20],
// ["TWENTY", 60],
// ["ONE HUNDRED", 100]]

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

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.157 Safari/537.36.

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/javascript-algorithms-and-data-structures-projects/cash-register/

1 Like

You’ve run into a common math problem that most (if not all) programming languages, including JavaScript have.

I highly, highly, highly recommend you go through https://floating-point-gui.de/ (and definitely run 0.1 + 0.2 in your console to see what the author is talking about). Then read http://adripofjavascript.com/blog/drips/avoiding-problems-with-decimal-math-in-javascript.html .

Once you’ve gone through those two resources, you should have a much better understanding of the underlying problem and how to account for it.

2 Likes

Thanks for taking your time to help with something that basic, your answer was really helpful to learn and complete this challenge!

1 Like