Format a large number for British readers.
Acceptance criteria
- 1.#out should show 1,234,567.
JavaScript Tutorial · JavaScript Numbers & Math
These 10 tasks go with the JavaScript Number Formatting 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: “toLocaleString formats numbers as currency, percentages and grouped digits.” .
1.Group the digitsexercise · easy · 10 min
Format a large number for British readers.
Passes when: #out should show 1,234,567.
2.Format as currencyexercise · easy · 10 min · practice plan
Show the price in pounds.
Passes when: #out should show £1,234.50.
3.Format a percentageexercise · easy · 10 min · practice plan
Show a quarter as a percentage with no decimals.
Passes when: #out should show 25%.
4.Keep trailing zerosexercise · easy · 10 min · practice plan
Show a whole number with two decimal places.
Passes when: #out should show 5.00.
5.Reuse a formatterexercise · medium · 10 min · practice plan
Build one Intl formatter and use it twice.
Passes when: #out should show £99.00.
6.Fix the percentage passed as a whole numberbug fix · easy · 8 min
Percent style multiplies by one hundred.
Passes when: #out should show 25%.
7.Fix the missing currency codebug fix · easy · 8 min · practice plan
Currency style needs the currency named.
Passes when: #out should show the price in pounds.
8.Fix toFixed used for groupingbug fix · easy · 8 min · practice plan
toFixed sets decimals but never groups the digits.
Passes when: #out should show 1,234,567.
9.Fix the trailing zeros droppedbug fix · easy · 8 min · practice plan
A whole number needs a minimum number of decimals.
Passes when: #out should show 5.00.
10.Fix the currency built by handbug fix · easy · 8 min · practice plan
Joining a symbol misses the digit grouping.
Passes when: #out should show the grouped price in pounds.
Between them these tasks cover toLocaleString, Currency and Intl.
Looking for a different chapter? See every practice set, or switch to the interview theory questions.