Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Selected Reading
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']
};
console.log(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 need to write a JavaScript function that takes an alphabet string and returns the number combination pressed to type that string.
Problem Example
For the word "mad":
- 'm' is on key 6
- 'a' is on key 2
- 'd' is on key 3
So the output should be [6, 2, 3].
Solution Approach
We'll create a reverse mapping from letters to numbers, then convert each character in the input string to its corresponding number.
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']
};
const convertToNumeral = (str = '') => {
// Create reverse mapping: letter -> number
const letterToNumber = {};
Object.entries(mapping).forEach(([number, letters]) => {
letters.forEach(letter => {
letterToNumber[letter] = parseInt(number);
});
});
// Convert each character to its corresponding number
return Array.from(str.toLowerCase(), char => letterToNumber[char]);
};
// Test with example
console.log(convertToNumeral('mad'));
console.log(convertToNumeral('hello'));
console.log(convertToNumeral('world'));
[ 6, 2, 3 ] [ 4, 3, 5, 5, 6 ] [ 9, 6, 8, 5, 3 ]
Alternative Implementation
Here's a more concise version using reduce:
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']
};
const convertToNumeralCompact = (str = '') => {
const letterToNumber = Object.entries(mapping).reduce((acc, [number, letters]) => {
letters.forEach(letter => acc[letter] = +number);
return acc;
}, {});
return Array.from(str.toLowerCase(), char => letterToNumber[char]);
};
console.log(convertToNumeralCompact('javascript'));
[ 5, 2, 8, 2, 7, 2, 7, 4, 7, 8 ]
How It Works
- Reverse Mapping: We create a lookup object where each letter maps to its corresponding number key
- String Processing: Convert the input to lowercase and iterate through each character
- Number Lookup: For each character, find its corresponding number from our reverse mapping
- Result Array: Return an array of numbers representing the keys pressed
Conclusion
This solution efficiently converts words to their corresponding phone keypad numbers by creating a reverse lookup table. The function handles both uppercase and lowercase input by normalizing to lowercase.
Advertisements
