JavaScript parseInt and parseFloat — exercises and bug fixes
5 hands-on exercises and 5 find-the-bug challenges for the JavaScript parseInt and parseFloat 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
Read a number from text
easy10 minPull the number out of a CSS width.
- 2
Keep the decimal part
easy10 minRead the price from text that has words after it.
- 3
Use a different base
easy10 minRead a hexadecimal value.
- 4
Handle unreadable text
easy10 minReport a message when nothing can be read.
- 5
Compare with Number
medium10 minShow that Number rejects what parseInt accepts.
Find the bug(5)
- 1
Fix Number used on a suffixed value
easy8 minNumber needs the whole string to be numeric.
- 2
Fix parseInt losing the decimals
easy8 minparseInt truncates, so the pence disappear.
- 3
Fix the missing radix
easy8 minHexadecimal text needs a base of sixteen.
- 4
Fix the unchecked NaN
easy8 minA failed parse gives NaN, which must be checked with Number.isNaN.
- 5
Fix parseInt used for validation
easy8 minparseInt accepts junk after the number, so it is too lenient here.
Ready to write the code?
The editor, the automatic checks and the worked solutions open up with an account.
Open the workspace