Code not working as expected

Tell us what’s happening:
Describe your issue in detail here.
Hello, I am having trouble with the cash register challenge, where my code isn’t working as expected. (not done yet)

  **Your code so far**

function checkCashRegister(price, cash, cid) {
var left = cash - price;
var reverse = cid.reverse();
var leftover;
var amount;
var arr = [];
var arr2 = [];
var change = [];
// don't forget to unreverse it later.
for (var i = 0; i < reverse.length; i++) {
  leftover = reverse[i][1];
  var used = 0;
  switch(reverse[i][0]) {
    case "ONE HUNDRED":
      amount = 100;
      break;
    case "TWENTY":
      amount = 20;
      break;
    case "TEN":
      amount = 10;
      break;
    case "FIVE":
      amount = 5;
      break;
    case "ONE":
      amount = 1;
      break;
    case "QUARTER":
      amount = 0.25;
      break;
    case "DIME":
      amount = 0.10;
      break;
    case "NICKEL":
      amount = 0.05;
      break;
    case "PENNY":
      amount = 0.01;
      break;
  }
  while (leftover - amount >= 0 && left - amount >= 0) {
      left = left - amount;
      leftover = leftover - amount;
      used = used + amount;
  }
  arr.push(used); 
  arr2.push(leftover);
  console.log(arr)
  console.log(left)
}

  
}


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

In this code, the “left” variable is used to see how much more change we need. The while loop continues only if the variable leftover (variable used to check how much of the currency is left in the drawer) minus variable “amount” (used to check how much each currency is worth) is greater than or equal to 0 and if “left” minus “amount” is greater or equal to 0.

In the final console, the variable “left” was 0.009999999999994869. The amount used in the “PENNY” array was 0.03, instead of 0.04. If anybody can give me advice on why this happened, I would really appreciate it. Thanks!

  **Your browser information:**

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

Challenge: Cash Register

Link to the challenge:

Decimal numbers have round off. I would instead use an integer number of cents.

1 Like

I’m sorry, what do you mean by an integer number of cents? I’ve never heard of that before. Sorry for the inconvenience ;D

You are doing the calculations in terms of $0.01 instead of 1 cent. Decimals have rounding error on computers.

OK. So should I multiply everything by 100 so that the penny is 1?
(P.S. I’m sorry)

Yeah, that is what I would do. There are other ways to fix this problem, but doing everything in cents is the easiest solution to me.

Basically, numbers we use day to day are base 10, but computers represent all numbers in base 2. The difference leads to this rounding issue.

No need to apologize for asking questions. It’s what we’re here for!

1 Like

Thanks for all your help :>

1 Like

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