- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Find what numbers were pressed to get the word (opposite of phone number digit problem) in JavaScript
The mapping of the numerals to alphabets in the old keypad type phones used to be like this −
const mapping = { 1: [], 2: ['a', 'b', 'c'], 3: ['d', 'e', 'f'], 4: ['g', 'h', 'i'], 5: ['j', 'k', 'l'], 6: ['m', 'n', 'o'], 7: ['p', 'q', 'r', 's'], 8: ['t', 'u', 'v'], 9: ['w', 'x', 'y', 'z'] };
We are required to write a JavaScript function that takes in an alphabet string and return the number combination pressed to type that string.
For example −
If the alphabet string is −
const str = 'mad';
Then the output number should be −
const output = [6, 2, 3];
Example
The code for this will be −
const mapping = { 1: [], 2: ['a', 'b', 'c'], 3: ['d', 'e', 'f'], 4: ['g', 'h', 'i'], 5: ['j', 'k', 'l'], 6: ['m', 'n', 'o'], 7: ['p', 'q', 'acc', 's'], 8: ['t', 'u', 'v'], 9: ['w', 'x', 'y', 'z'] }; const convertToNumeral = (str = '') => { const entries = Object.entries(mapping); const res = entries.reduce((acc, [v, letters]) => { letters.forEach(l => acc[l] = +v); return acc; }, {}); const result = Array.from(str, (el) => { return res[el]; }); return result; }; console.log(convertToNumeral('mad'))
Output
And the output in the console will be −
[ 6, 2, 3 ]
- Related Articles
- How to get phone number in android?
- Find the largest palindrome number made from the product of two n digit numbers in JavaScript
- Get the count of unique phone numbers from a column with phone numbers declared as BIGINT type in MySQL
- How to get default phone number in android?
- How to find out what character key is pressed in JavaScript?
- Converting array to phone number string in JavaScript
- Word Break Problem
- Word Wrap Problem
- How to obtain the phone number of the iOS phone programmatically?
- How to obtain the phone number of the android phone programmatically?
- How to get phone number from content provider in android?
- Digit distance of two numbers - JavaScript
- Sorting numbers according to the digit root JavaScript
- Finding the nth digit of natural numbers JavaScript
- Is the digit divisible by the previous digit of the number in JavaScript

Advertisements