Have you tried logging this variable after it is created?
Have you also tried logging the cashToChange value inside the while loop?
Add more logs to discover what is happening
Have you tried logging this variable after it is created?
Have you also tried logging the cashToChange value inside the while loop?
Add more logs to discover what is happening
it doesn’t look like you are doing anything about floating point errors… make sure you are working with integers as much as possible (that means converting your money values to an integer number of cents). Logging your numbers you may find weird values, and this will counteract that,
Floating point error example: print the value of 0.2 + 0.1
I just tried, and it seems like an error
This is the reversedCid variable:

and this is the cashToChange inside the loop when the cash.value is 5 and the price is 1.87:

The questions is, how do i fix the reverserCid variable, i cant think of a solution, can you give me a hint?
it looks like you are changing it, that’s why it’s different
cashToChange will benefit from using an integer number of pennies to avoid floating point error
Hmm… can you explain what this means, i don’t understand it ![]()
I see that its changing, but i don’t know why?
you are subtracting billValue[i] from reversedCid[i], why do you expect reversedCid[i] to not change?
Oh, yeah, it should change so that i get the amount of money that i have left on the drawer
But i still get the status: Insufficient funds with whatever value that is greater than the price
Edit: if i remove this part of the code
if(cashToChange > 0){
change.textContent = "Status: INSUFFICIENT_FUNDS"
}
the status will be fine
you need to make calculations with an integer number of cents, instead of dollars, so to avoid floating point error. Otherwise your cashToChange is never reaching 0 but staying at whatever infinitesimal amount is remaining
0.0099 etc is smaller than any denomination but bigger than 0 so you get in this issue
What if i float the cashToChange to 2 digits after the comma?
using an integer number of cents is the proved way, even banks use that. It’s up to you how to write your code tho
Oh, okay. By integer number you mean what? You said that i should not use dollars (decimal) right?
an integer number of pennies (of 0.01 coins if you want), so that all your numbers are integers
Oh, i understand, so if i have $1.01 in penies that would be 101 as an integer?
And how can i convert it to dollars?
Lets say that the change due is 1042, how do i know that it is $100.2 and not $10.42?
if it was 100.2$ then it would be 10020 pennies
Hmm… im gonna take a look at how to modify my code to work like this. Thanks
Also, what if i have to change only 1 penny, how would that be as an integer?
it would be 1, it’s 1 penny
Oh, yeah, cuz i would convert it to a penny by dividing the whole change by 100 right?
if you divide 0.01 by 100 you get 0.0001. Try the opposite operation