Indexing numbers to alphabets in JavaScript

JavascriptWeb DevelopmentFront End TechnologyObject Oriented Programming

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
raja
Published on 23-Nov-2020 11:18:48
Advertisements