
- 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
Mapping string to Numerals in JavaScript
We are required to write a JavaScript function that takes in a string. It should print out each number for every corresponding letter in the string.
For example:
a = 1 b = 2 c = 3 d = 4 e =5 . . . y = 25 z = 25
Note: Remove any special characters and spaces.
So, if the input is −
"hello man"
Then the output should be −
"8,5,12,12,15,13,1,14"
Example
The code for this will be −
const str = 'hello man'; const charPosition = str => { str = str.split(''); const arr = []; const alpha = /^[A-Za-z]+$/; for(i=0; i < str.length; i++){ if(str[i].match(alpha)){ const num = str[i].charCodeAt(0) - 96; arr.push(num); }else{ continue; }; }; return arr.toString(); } console.log(charPosition(str));
Output
The output in the console will be −
"8,5,12,12,15,13,1,14"
- Related Articles
- Filtering out numerals from string in JavaScript
- Mapping unique characters of string to an array - JavaScript
- Expanding Numerals in JavaScript
- Mapping the letter of a string to an object of arrays - JavaScript
- Mapping values to keys JavaScript
- Decrypt String from Alphabet to Integer Mapping in Python
- Python – Character indices Mapping in String List
- Reverse mapping an object in JavaScript
- Java Program to create String to super class type mapping
- JavaScript map value to keys (reverse object mapping)
- Number to Roman Numerals
- Mapping an array to a new array with default values in JavaScript
- Mapping array of numbers to an object with corresponding char codes in JavaScript
- Explain roman numerals.
- Map an integer from decimal base to hexadecimal with custom mapping JavaScript

Advertisements