Hi, would you mind guiding me a little on this, I have looked at StackOverflow, Geek4Geeks and generally searched the web but I am at a complete loss. I have been struggling with this part of the task:

'So we'll then round the result to two digits after a decimal point.

If the number exceeds 999.99T it becomes 1.00aa, after 999.99aa goes 1.00ab.
When the number gets as high as 999.99az it will next turn into 1.00ba and so on.'

Below is what I have so far, it returns numbers 5 and 7 as failing the test, I'm not sure if I'm over complicating this?

export const formatNumber = (n) => {
  let number = n;
  let suffix = [97, 92];
  let newNumber = "";

  if (n < 1000) {
        return n.toFixed(1);
      } else if (n < 1000000) {
          return (n / 1000).toFixed(2) + 'K';
      } else if (n < 1000000000) {
          return (n / 1000000).toFixed(2) + 'M'; 
      } else if (n < 1000000000000) {
          return (n / 1000000000).toFixed(2) + 'B';
      } else if (n < 1000000000000000){
          return (n / 1000000000000).toFixed(2) + 'T';
      } 

  while (number > 1000) {
    (number /= 1000);
    if (suffix[1] < 122) {
      suffix[1]++;
    } else {
      suffix[0]++;
      suffix[1] = 97
    } 
  }

  const stringNumber = String(number);
  const i = stringNumber.indexOf(".");
  
  if (i >= 0) {
    newNumber = stringNumber.substring(0, i) + "." + stringNumber.substring(i + 1, i + 3);
  } else {
    newNumber = stringNumber + ".00";
  }  
     
  return newNumber + String.fromCharCode(suffix[0]) + String.fromCharCode(suffix[1]);
}

    Jesse It's a tough task, you'll feel great when you eventually crack it 🙂

    Here are 2 hints:
    1) Check how your code works with the number 4.50ab. Does it cut the trailing zero?
    2) Is there unnecessary rounding for the number 6.55 * 1e51? It should become 6.55am and not 6.54am.

      5 days later
      Write a Reply...