
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
Number to alphabets in JavaScript
We are required to write a JavaScript function that takes in a string of any variable length that represents a number.
Our function is supposed to convert the number string to the corresponding letter string.
For example − If the number string is −
const str = '78956';
Then the output should be −
const output = 'ghief';
If the number string is −
const str = '12345';
Then the output string should be −
const output = 'lcde';
Notice how we didn't convert 1 and 2 to alphabets separately because 12 also represents an alphabet. So we have to consider this case while writing our function.
We, here, assume that the number string will not contain 0 in it, if it contains though, 0 will be mapped to itself.
Example
Let us write the code for this function −
const str = '12345'; const str2 = '78956'; const convertToAlpha = numStr => { const legend = '0abcdefghijklmnopqrstuvwxyz'; let alpha = ''; for(let i = 0; i < numStr.length; i++){ const el = numStr[i], next = numStr[i + 1]; if(+(el + next) <= 26){ alpha += legend[+(el + next)]; i++; } else{ alpha += legend[+el]; }; }; return alpha; }; console.log(convertToAlpha(str)); console.log(convertToAlpha(str2));
Output
And the output in the console will be −
lcde ghief
- Related Articles
- Indexing numbers to alphabets in JavaScript
- Converting alphabets to Greek letters in JavaScript
- Crack Alphabets fight problem in JavaScript
- Converting a string to NATO phonetic alphabets in JavaScript
- Sorting alphabets within a string in JavaScript
- Decreasing order sort of alphabets in JavaScript
- Reversing alphabets in a string using JavaScript
- Replace alphabets with nth forward alphabet in JavaScript
- Transforming array of numbers to array of alphabets using JavaScript
- Padding a string with random lowercase alphabets to fill length in JavaScript
- How to separate alphabets and numbers from an array using JavaScript
- Counting the number of letters that occupy their positions in the alphabets for array of strings using JavaScript
- Converting numbers into corresponding alphabets and characters using JavaScript
- Finding the 1-based index of a character in alphabets using JavaScript
- Finding number of alphabets, digits and special characters in strings using C language

Advertisements