Pull the number out of a CSS width.
Acceptance criteria
- 1.#out should show 250.
JavaScript Tutorial · JavaScript Numbers & Math
These 10 tasks go with the JavaScript parseInt and parseFloat 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: “parseInt and parseFloat read a number from the start of a string.” .
1.Read a number from textexercise · easy · 10 min
Pull the number out of a CSS width.
Passes when: #out should show 250.
2.Keep the decimal partexercise · easy · 10 min · practice plan
Read the price from text that has words after it.
Passes when: #out should show 19.99.
3.Use a different baseexercise · easy · 10 min · practice plan
Read a hexadecimal value.
Passes when: #out should show 255.
4.Handle unreadable textexercise · easy · 10 min · practice plan
Report a message when nothing can be read.
Passes when: #out should show no number there.
5.Compare with Numberexercise · medium · 10 min · practice plan
Show that Number rejects what parseInt accepts.
Passes when: #out should show rejected.
6.Fix Number used on a suffixed valuebug fix · easy · 8 min
Number needs the whole string to be numeric.
Passes when: #out should show 250.
7.Fix parseInt losing the decimalsbug fix · easy · 8 min · practice plan
parseInt truncates, so the pence disappear.
Passes when: #out should show 19.99.
8.Fix the missing radixbug fix · easy · 8 min · practice plan
Hexadecimal text needs a base of sixteen.
Passes when: #out should show 255.
9.Fix the unchecked NaNbug fix · easy · 8 min · practice plan
A failed parse gives NaN, which must be checked with Number.isNaN.
Passes when: #out should show no number there.
10.Fix parseInt used for validationbug fix · easy · 8 min · practice plan
parseInt accepts junk after the number, so it is too lenient here.
Passes when: #out should show rejected.
Between them these tasks cover parseInt, parseFloat and Radix.
Looking for a different chapter? See every practice set, or switch to the interview theory questions.