Format the price to two decimal places.
Acceptance criteria
- 1.#out should show 19.57.
JavaScript Tutorial · JavaScript Numbers & Math
These 10 tasks go with the JavaScript Number Methods 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: “Number methods format and convert numbers, always returning a new value.” .
1.Round to two decimalsexercise · easy · 10 min
Format the price to two decimal places.
Passes when: #out should show 19.57.
2.Keep trailing zerosexercise · easy · 10 min · practice plan
Format a whole number as money.
Passes when: #out should show 5.00.
3.Convert to binaryexercise · easy · 10 min · practice plan
Report 255 in base two.
Passes when: #out should show 11111111.
4.Convert to hexexercise · easy · 10 min · practice plan
Report 255 in base sixteen.
Passes when: #out should show ff.
5.Convert the result backexercise · medium · 10 min · practice plan
Round to two decimals then add one as a number.
Passes when: #out should show 20.57.
6.Fix the string result added tobug fix · easy · 8 min
toFixed returns a string, so plus joins instead of adding.
Passes when: #out should show 20.57.
7.Fix round used instead of toFixedbug fix · easy · 8 min · practice plan
Math.round loses the trailing zeros that money needs.
Passes when: #out should show 5.00.
8.Fix the missing basebug fix · easy · 8 min · practice plan
Without a base, toString gives the ordinary decimal form.
Passes when: #out should show 11111111.
9.Fix toPrecision used for decimalsbug fix · easy · 8 min · practice plan
toPrecision counts all digits, not just the decimals.
Passes when: #out should show 19.57.
10.Fix the wrong number of decimalsbug fix · easy · 8 min · practice plan
Money needs two decimal places, not one.
Passes when: #out should show 19.57.
Between them these tasks cover toFixed, toString and Base conversion.
Looking for a different chapter? See every practice set, or switch to the interview theory questions.