Indexing numbers to alphabets in JavaScript


We are required to write a JavaScript function that takes in a number between the range [0, 25], both inclusive.

Return value

The function should return the corresponding alphabets for that number.

Example

The code for this will be −

const num = 15;
const indexToAlpha = (num = 1) => {
   // ASCII value of first character
   const A = 'A'.charCodeAt(0);
   let numberToCharacter = number => {
      return String.fromCharCode(A + number);
   };
   return numberToCharacter(num);
};
console.log(indexToAlpha(num));

Output

And the output in the console will be −

P

Updated on: 23-Nov-2020

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements