numbers and alphabet char code js code example
Example 1: javascript letters as number
const toChars = n => `${n >= 26 ? toChars(Math.floor(n / 26) - 1) : ''}${'ABCDEFGHIJKLMNOPQRSTUVWXYZ'[n % 26]}`;
toChars(0);
toChars(1);
toChars(25);
Example 2: javascript alphabet to number
function convertLetterToNumber(str) {
str = str.toUpperCase();
let out = 0, len = str.length;
for (pos = 0; pos < len; pos++) {
out += (str.charCodeAt(pos) - 64) * Math.pow(26, len - pos - 1);
}
return out;
}
convertLetterToNumber("A");
convertLetterToNumber("b");
convertLetterToNumber("Ba");
convertLetterToNumber("dE");
convertLetterToNumber("ZZZZ");