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

Still stuck on this #freeCodeCamp #JavaScript project. I've used console.log statements to look at what different functions are returning, but I don't understand why they're returning what they are for test 18 and 19. Can someone please help? I just posted my code on the freeCodeCamp forum, and you can find it at: https://forum.freecodecamp.org/t/build-a-cash-register-project-build-a-cash-register/717252
#JS #programming #coding #learningToCode #learningToProgram #beginnerProgrammer #beginnerCoder #JavaScriptIsHard Good evening all. Just woke up and had something to eat, leftover pizza from yesterday. Going to get dressed and then try to tackle this #freeCodeCamp #JavaScript project again. Still feeling a little overwhelmed and unsure how to fix things, but hopefully it will go better now that I've slept some.
#JS #programming #coding #learningToCode #learningToProgram #beginnerCoder #beginnerProgrammer #JavaScriptIsHard
Build a Cash Register Project - Build a Cash Register

Tell us what’s happening: My code is failing tests 18 and 19. I used console.log statements to figure out why, and it’s returning INSUFFICIENT-FUNDS for test 18. I’ve gone over my code but have no idea why. Can someone help me figure this out? Your code so far <!DOCTYPE html> <html lang="en"> <head> <meta name="viewport" content="width=device-width, initial-scale = 1.0" /> <meta charset="UTF-8" /> <title>freeCodeCamp Cash Register Project</title> </head> <body> <h1>Cash Re...

The freeCodeCamp Forum
@RareBird_15 If I'm reading this right, you are getting the correct result, the problem is formatting it - they want you to filter out the zero values and only show the Penny value
@earthtoneone Thanks. I think I'm also getting floating point errors for things and that's causing issues. A couple people have suggested switching functions to using cents instead of dollars, and I tried that last night but must have messed up somewhere because it made my code fail a lot of tests.

@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.