DEV Community

JonMichael Dreher
JonMichael Dreher

Posted on

Numbers to Letters Code Convert Numbers to Letters JavaScript

Convert a letter to a number and vice-versa.

tl,dr:

ASCII Table

Number to letter:

let findNum = 22;
let num = String.fromCharCode(97 + findNum)
console.log(num)
> w

Letter to number:

let uni = 'y'.charCodeAt(0)
console.log(uni)
> 121

Basis of solution: ASCII and String prototype and its methods.
What does ASCII stand for?
American Standard Code for Information Interchange: Basically, symbols we use on a computer that are mapped to a number. For example:
ASCII CODE 97 : a
ASCII CODE 98 : b
ASCII CODE 99 : c
ASCII CODE 110 : n
etc, etc, etc...
How do we use it in JavaScript?
String prototype and the fromCharCode() method on the prototype.
fromCharCode() i.e. "from character code" with String = "Convert this number, from the character code I am providing you, into a letter."
Because our alphabet in the ASCII table starts at 97 our charCode = 97 + "number we provide" for our cases, between 97-122, a-z.
let findNum = 22;
let num = String.fromCharCode(97 + findNum)
console.log(num)
> w
Convert Letter to Number
Sidenote: ASCII is a part of the Unicode standard. Unicode is a massive list of thousands of symbol maps, and ASCII is included within the Unicode system(first grader explanation; sorry in advance for offending really smart first-graders).
So if we have the letter 'y'.
let uni = 'y'.charCodeAt(0)0 being the index of the character of the string of which we want.
console.log(uni)
>121
And that's it - Thanks for reading! Comments very very welcome, lots to learn here!

Discussion (0)