CashRegister: Missing my calculation by "0.01". Can anyone please suggest

Tell us what’s happening:

The final Calculation is altered by “0.01”. My final value in to loop fails to capture due to this alteration. Please suggest

Your code so far


function checkCashRegister(price, cash, cid) {
const fundStatus = {open:"OPEN",closed:"CLOSED",insufficient:"INSUFFICIENT_FUNDS"}

const CurrencyUnit = {
  "PENNY": 0.01,
  "NICKEL":0.05,
  "DIME":0.1,
  "QUARTER":0.25,
  "ONE":1,
  "FIVE":5,
  "TEN":10,
  "TWENTY":20,
  "ONE HUNDRED":100
}

var change = (cash-price).toFixed(2);
const changeNeeded = change;
var totalCash = totalCashInDrawer(cid);
var changeObj = {status:"",change:[]}

console.log(change+":"+totalCash)
if(Number(change) > Number(totalCash)){
  changeObj.status = fundStatus.insufficient;
  changeObj.change = [];
}

if(Number(change) == Number(totalCash)){
  changeObj.status = fundStatus.closed;
  changeObj.change = cid;
}

if(Number(change) < Number(totalCash)){
  let total = [];
  for(let j=cid.length-1;j>=0;j--){
    let count =0;let cu = cid[j][0];
    while(change >= CurrencyUnit[cu] && cid[j][1]>0){
     
      change -= CurrencyUnit[cu]
      cid[j][1] -= CurrencyUnit[cu]
      count++;
      
    }

     if(count*CurrencyUnit[cu]!=0){
          total.push([cu,count*CurrencyUnit[cu]]);
     }
  }

  let totalreturnSum = 0;
  for(let i=0;i<total.length;i++){
    totalreturnSum+= total[i][1];
  }

console.log(change)
  if(totalreturnSum != changeNeeded){
    changeObj.status = fundStatus.insufficient;
    changeObj.change = [];
  }else if(total != ""){
    changeObj.status = fundStatus.open;
    changeObj.change = total;
  }
}
return changeObj;
}

function totalCashInDrawer(cashInDrawer){
var total = 0;
for(let i=0;i<cashInDrawer.length;i++){
  total+=cashInDrawer[i][1];
}

return total.toFixed(2);
}

console.log(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]]));

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36 Edg/87.0.664.66.

Challenge: Cash Register

Link to the challenge:

Took me a while to debug :slight_smile:

Well done! Only one test was failing. You seem to be missing a couple of Math.round calls.

  change = Math.round(change * 100) / 100
  totalreturnSum = Math.round(totalreturnSum * 100) / 100

I changed a few other things while debugging, but I guess the problem was only those two missing lines. This is not an easy problem, and I could only figure it out by comparing your answer to the provided solution.

Here’s the full working snippet:

function checkCashRegister(price, cash, cid) {
const fundStatus = {open:"OPEN",closed:"CLOSED",insufficient:"INSUFFICIENT_FUNDS"}

const CurrencyUnit = {
  "PENNY": 0.01,
  "NICKEL":0.05,
  "DIME":0.1,
  "QUARTER":0.25,
  "ONE":1,
  "FIVE":5,
  "TEN":10,
  "TWENTY":20,
  "ONE HUNDRED":100
}

var change = cash-price;
const changeNeeded = change;
var totalCash = totalCashInDrawer(cid);
var changeObj = {status:"",change:[]}

console.log("line 21: ", "change: " ,change+" Total: "+totalCash)
if(Number(change) > Number(totalCash)){
  changeObj.status = fundStatus.insufficient;
  changeObj.change = [];
}

if(Number(change) === Number(totalCash)){
  changeObj.status = fundStatus.closed;
  changeObj.change = cid;
}

if(Number(change) < Number(totalCash)){
  let total = [];
  for(let j=cid.length-1;j>=0;j--){
    let count =0;let cu = cid[j][0];
    while(change >= CurrencyUnit[cu] && cid[j][1]>0){
     
      change = change - CurrencyUnit[cu]
      change = Math.round(change * 100) / 100
      cid[j][1] = cid[j][1] - CurrencyUnit[cu]
      count++;
      
    }

     if(count*CurrencyUnit[cu]!=0){
          total.push([cu,count*CurrencyUnit[cu]]);
     }
  }

  let totalreturnSum = 0;
  for(let i=0;i<total.length;i++){
    totalreturnSum = totalreturnSum + total[i][1];

  }

  totalreturnSum = Math.round(totalreturnSum * 100) / 100

console.log("change: ", change)

  if(totalreturnSum != changeNeeded){
    console.log("totalreturnSum", totalreturnSum)
    console.log("changeNeeded", changeNeeded)

    changeObj.status = fundStatus.insufficient;
    changeObj.change = [];
  }else if(total != ""){
    changeObj.status = fundStatus.open;
    changeObj.change = total;
  }
}
return changeObj;
}

function totalCashInDrawer(cashInDrawer){
var total = 0;
for(let i=0;i<cashInDrawer.length;i++){
  total+=cashInDrawer[i][1];
}

return total;
}

console.log(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

Hey @SourceCodeSchool.Com!

It is great that you solved the challenge, but instead of posting your full working solution, it is best to stay focused on answering the original poster’s question(s) and help guide them with hints and suggestions to solve their own issues with the challenge.

We are trying to cut back on the number of spoiler solutions found on the forum and instead focus on helping other campers with their questions and definitely not posting full working solutions.

You can post solutions that invite discussion (like asking how the solution works, or asking about certain parts of the solution). But please don’t just post your solution for the sake of sharing it.
If you post a full passing solution to a challenge and have questions about it, please surround it with [spoiler] and [/spoiler] tags on the line above and below your solution code.

@jwilkins.oboe please edit the post.

1 Like

Hi @SourceCodeSchool.Com!

I know you are new to the forum and welcome.

All of the campers have access to the full answer guide so that is why we discourage handing out answers on the forum.

It is a better educational experience to help lead them to the right answer through discussion instead of providing full working solutions.

Hope you understand.

Thanks!

1 Like

Thank you all. The only problem was with the Math.round(). Thanks for looking into all this codes.

1 Like

@jwilkins.oboe Thanks for reminding me the rules, yes that was a rookie mistake, it won’t happen again :slight_smile:

1 Like

Another way to handle this is to instead use an integer number of cents instead of a decimal number of dollars. Decimals have roundoff issues in all programming languages due to how decimal numbers are stored, added, multiplied, etc in binary.

2 Likes

Expanding on what @JeremyLT said, try typing the following into your JS console:

0.3 - 0.2

Never use floating point numbers (decimals) for money if exact change is required :slight_smile:

2 Likes

Can you provide such cases?
I my code for this I used Math.round(), it passed all fcc tests
I used Math.floor() also in a couple of lines

Orrr… You bypass all that fuss by using an integer number of cents.

1 Like