JavaScript Number Methods — exercises and bug fixes
5 hands-on exercises and 5 find-the-bug challenges for the JavaScript Number Methods lesson. Every one is checked automatically in the browser — write the code, run it, and the tests tell you what still fails.
Exercises(5)
- 1
Round to two decimals
easy10 minFormat the price to two decimal places.
- 2
Keep trailing zeros
easy10 minFormat a whole number as money.
- 3
Convert to binary
easy10 minReport 255 in base two.
- 4
Convert to hex
easy10 minReport 255 in base sixteen.
- 5
Convert the result back
medium10 minRound to two decimals then add one as a number.
Find the bug(5)
- 1
Fix the string result added to
easy8 mintoFixed returns a string, so plus joins instead of adding.
- 2
Fix round used instead of toFixed
easy8 minMath.round loses the trailing zeros that money needs.
- 3
Fix the missing base
easy8 minWithout a base, toString gives the ordinary decimal form.
- 4
Fix toPrecision used for decimals
easy8 mintoPrecision counts all digits, not just the decimals.
- 5
Fix the wrong number of decimals
easy8 minMoney needs two decimal places, not one.
Ready to write the code?
The editor, the automatic checks and the worked solutions open up with an account.
Open the workspace