-
Notifications
You must be signed in to change notification settings - Fork 15
/
Mnemonics.js
50 lines (39 loc) · 1.28 KB
/
Mnemonics.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
/*
Given a particular number say 637-8687 (NERVOUS) would be the word.
So for the older keypad’s seen on telephone’s I would have to create Mnemonics.
So for doing this, the first part being list out all the Permutations possible for a particular number series.
Ex: listMnemonics(“723″) would result in
PAD PBD PCD QAD QBD QCD RAD RBD RCD SAD SBD SCD
PAE PBE PCE QAE QBE QCE RAE RBE RCE SAE SBE SCE
PAF PBF PCF QAF QBF QCF RAF RBF RCF SAF SBF SCF
*/
var digitToAlpha = [
/* 0 */ "",
/* 1 */ "",
/* 2 */ "ABC",
/* 3 */ "DEF",
/* 4 */ "GHI",
/* 5 */ "JKL",
/* 6 */ "MNO",
/* 7 */ "PQRS",
/* 8 */ "TUV",
/* 9 */ "WXYZ"
]
function listMnemonics(number) {
var numberStr = number.toString(),
currentMnemonics = [],
nextMnemonics = [],
mnemonics = [];
if (numberStr.length === 0) {
return [""];
}
currentMnemonics = digitToAlpha[numberStr[0]].split("");
nextMnemonics = listMnemonics(numberStr.slice(1));
for (var i = 0, currentLen = currentMnemonics.length; i < currentLen; i++) {
for (var j = 0, nextLen = nextMnemonics.length; j < nextLen; j++) {
mnemonics.push(currentMnemonics[i] + nextMnemonics[j]);
}
}
return mnemonics;
}
console.log(listMnemonics(723));