Report whether adding a tenth to two tenths gives exactly three tenths.
Acceptance criteria
- 1.#out should show not exact.
JavaScript Tutorial · JavaScript Numbers & Math
These 10 tasks go with the JavaScript Number Precision chapter. 5 of them ask you to build something small from scratch; 5 give you code that is already broken and ask you to work out why. Set aside about 90 minutes for the whole set, though you can do them in any order.
Nothing here is marked by a person. When you press Submit, the editor checks your code against the points listed under each task and tells you straight away what passed and what didn't (you need a free account to submit). If you get stuck, there are hints, and a worked solution once you've had a go. The first 2 tasks are free; the rest are part of the practice plan.
If it's been a while since you read the chapter, here is how it starts: “Decimal arithmetic in JavaScript is not always exact, which is why 0.1 plus 0.2 is not 0.3.” .
1.See the precision errorexercise · easy · 10 min
Report whether adding a tenth to two tenths gives exactly three tenths.
Passes when: #out should show not exact.
2.Compare with a toleranceexercise · easy · 10 min · practice plan
Check the difference is small enough instead.
Passes when: #out should show close enough.
3.Round for displayexercise · easy · 10 min · practice plan
Show the total rounded to two decimal places.
Passes when: #out should show 0.30.
4.Work in whole numbersexercise · easy · 10 min · practice plan
Add the values as pence and convert back to pounds.
Passes when: #out should show 0.3.
5.Show whole numbers are exactexercise · medium · 10 min · practice plan
Report whether one plus two is exactly three.
Passes when: #out should show exact.
6.Fix the exact decimal comparisonbug fix · easy · 8 min
Decimals should be compared with a tolerance.
Passes when: #out should show close enough.
7.Fix the accumulated driftbug fix · easy · 8 min · practice plan
Adding a tenth ten times does not give exactly one.
Passes when: #out should show 1.00.
8.Fix the money stored as poundsbug fix · easy · 8 min · practice plan
Storing pence keeps the arithmetic exact.
Passes when: #out should show exact result.
9.Fix the tolerance that is too tightbug fix · easy · 8 min · practice plan
A tolerance of zero is the same as an exact comparison.
Passes when: #out should show close enough.
10.Fix the missing absolute valuebug fix · easy · 8 min · practice plan
Without abs, a negative difference slips under any tolerance.
Passes when: #out should show spotted the gap.
Between them these tasks cover Floating point, Tolerance and Whole numbers.
Looking for a different chapter? See every practice set, or switch to the interview theory questions.