DEV Community


Posted on • Updated on

Letters of the Alphabet Numbered Get alphabet position value of a letter [JavaScript]

a = 1, b = 2 ... z = 26 if we need these decimal values (alphabet position) of a letter. One of the solutions is to use UTF (ASCII) Table.

ASCII table contains the decimal/hex/binary representation of each character (letter, number, symbol ...) of your keyboard.

Alt Text

If you see this table 'a' represents 97 number and z represents 122, so alphabet starts at 97 number but we need 1 instead of 97, it's easy, the only thing we need to do is to subtract '96' for each letter representation.

JavaScript code

In order to get UTF code of a character, we use charCodeAt string function.

chatCodeAt returns UTF 16 decimal representation of one character into a string.

'hello'.charCodeAt(1) - 96 // output 5

//This takes only 1 parameter, index of the character we want.
//remember index starts at 0 (first letter = 0, second = 1)
//here we  get 'e' decimal so it returns number 5.
Enter fullscreen mode Exit fullscreen mode

Use case

This is an example of algorithm problem

Each letter of a word scores points according to its 
position in the alphabet: a = 1, b = 2, c = 3 etc.

We need the highest scoring word as a string.
If two words score the same, return the word that
appears earliest in the original string.
All letters will be lowercase and all inputs will be valid.

Write a high function which takes a string and returns 
the highest score word


 assert.equal(high('man i need a taxi up to ubud'), 'taxi');
 assert.equal(high('what time are we climbing up the volcano'), 'volcano'); 
 assert.equal(high('take me to semynak'), 'semynak');  
Enter fullscreen mode Exit fullscreen mode

Top comments (0)