javascript letters as number 1

javascript letters as number

const toChars = n => `${n >= 26 ? toChars(Math.floor(n / 26) - 1) : ''}${'ABCDEFGHIJKLMNOPQRSTUVWXYZ'[n % 26]}`;

// Examples
toChars(0);     // A
toChars(1);     // B
toChars(25);    // Z

Here is what the above code is Doing:
1. The toChars function takes a number as an argument.
2. If the number is greater than or equal to 26, it calls itself with the number divided by 26 minus 1.
3. It then returns the result of the recursive call plus the letter at the index of the number modulo 26.
4. If the number is less than 26, it returns the letter at the index of the number modulo 26.

Similar Posts