I'm working on the cash register #JavaScript project from #freeCodeCamp (https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures-v8/build-a-cash-register-project/build-a-cash-register), and I almost have it, but I keep failing one or two of the tests. Right now, it's test 19. I added my #code to #JSBin, which is more #accessible than #CodePen, and it can be found here: https://jsbin.com/yiratay/edit?html,js,output
Can anyone help me understand what I'm doing wrong?
#JS #accessibility #learningToCode #learningToProgram #beginnerProgrammer #beginnerCoder #JavaScriptIsHard
freeCodeCamp.org

Learn to Code — For Free

@RareBird_15 Ohhh thats an nasty bug that you stumpled onto here, so let me explain.

First lets insert some debug statements to check whats going on: We'll add some console.log statements in the calculateChange function, about after the cash === price if, and print out the following:

  • totalCid + price
  • totalCid
  • change

If you now enter any value (like 2 for example), you'll get this in your console:

337.28000000000003
335.41
0.1299999999999999

Whats that enourmous (broken) looking numbers you might ask. That's an infamous bug when you work with floating point numbers. Floats have an precision; if you make calculations that involve something that goes beyond the precision, weird things happens. For example; just execute this line: 0.1 + 0.2, which every sane human being would say is 0.3 but computers instead produce 0.30000000000000004.

The same thing is happening to you here; since the total sum of your cid would equal to 337.28 in an calculator, computer-math instead results in 337.28000000000003. If you plug in that number instead, you'll see that it indeed responds with the correct output.

An article for further informations on that topic can be found here: https://jvns.ca/blog/2023/02/08/why-does-0-1-plus-0-2-equal-0-30000000000000004/ (Only the first one I've got of from google, there should be plenty others available, just search after "float precision" and "float rounding bugs" etc.)

Also, here's my jsbin with the log statements already applied: https://jsbin.com/kenodulowu/edit?js,console,output

Why does 0.1 + 0.2 = 0.30000000000000004?

Why does 0.1 + 0.2 = 0.30000000000000004?

Julia Evans
@mai_lapyst Thanks. This definitely helps. Would a good way to fix it be to use .toFixed(2) to convert the numbers to ones with only 2 decimal places?

@RareBird_15 Sadly no, because .toFixed returns an string, and you can't mathematically work with them, unless you'll use parseFloat to make a number out of them again, but then we're back at square one xD

An tip: the problematic part is the fractional part of the number :3

@mai_lapyst Hmm... Any suggestions on how to fix this then? Not asking for you to write my code or anything. Just asking for a hint as to how I can fix this and pass the tests.
@RareBird_15 The problematic part is the fraction, so we could simply multiply each value by 100 first, then round it to get rid of any decimal places we're not interested, to the calculation and divide by 100 at the very end to get back to an number with two decimal places :3
@mai_lapyst Lol ugh I'm getting frustrated. I try to fix it and it just fails more tests. Not liking programming too much right now.
@mai_lapyst I think I'm going to quit working on this for now and go to bed. I'm frustrated and tired, and everything I try just makes things worse.