You might have noticed, that there are occasional issues with displaying the numbers in our game.
Sometimes, there appear long weird tails after the decimal point.
The other times the numbers get so big that it’s hard to make sense of them.
Let’s implement the function formatNumber(n)
in functions.js
that should take the number and return a string representing an improved versions of it.
Here’s how it should work:
- If
n < 1000
, it should be rounded to a single digit after a decimal point - Else, to cut down the length of a number, we need to use letters ‘K’, ‘M’, ‘B’, ‘T’ to represent
thousands, millions, billions or trillions. We’re not really interested in being super precise here.
So we’ll then round the result to two digits after a decimal point.
Examples: 12352.1 => 12.35K, 1234321 => 1.23M, 12343210000000 => 12.34T
Change the output format of GOLD and PRODUCTION RATE using the formatNumber
.
You can include it into other places like the list of producers, although it’s not mandatory.
This task is part of the Full-Stack JavaScript Course.
If you have any issues with it, you can ask for community help below the post.
Feel free to help others if you’ve already solved the task.