Implement the function formatNumber
.
It’s essentially the same function that you’ve implemented in the end of the Gold Mine
game.
The difference is that here we want to handle really large numbers. We’ll add a suffix to then: (aa, ab, ac, …).
Here’s expected behavior:
- 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. - If the number exceeds
999.99T
it becomes1.00aa
, after999.99aa
goes1.00ab
. - When the number gets as high as
999.99az
it will next turn into1.00ba
and so on.
Examples: 12352.1 => 12.35K, 1234321 => 1.23M, 12343210000000 => 12.34T, 12343210000000000 => 12.34aa
12343210000000000000 => 12.34ab
Also, make sure to keep trailing zeroes. 5 * 1e12 should become 5.00T and not 5T.
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
helper.js
export const formatNumber = (n) => {
return n;
}
solution.js
import { formatNumber } from './helper.js';
const numbers = [];
for (let i = 0; i < numbers.length; i++) {
console.log(`Formatted ${numbers[i]} looks like ${formatNumber(numbers[i])}`);
}