Skip to content

Commit 80ca4c7

Browse files
committed
Mnemonics
1 parent c74fd4b commit 80ca4c7

File tree

1 file changed

+50
-0
lines changed

1 file changed

+50
-0
lines changed

Mnemonics.js

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

0 commit comments

Comments
 (0)