ic3b3rg
BAN USERJavaScript
(function(){ alert(getMnemonics("867-5309")); })();
function getMnemonics(phoneNumber) {
// we'll use a recursive method to grow our list of mnemonics
// the list will need a seed, so we'll start with a = [""]
//
var a = [""], i;
// we'll use a simple regex replace to get rid of any non-numeric input.
// if we need to match to a dictionary of words, what do we do with
// phone numbers that have "0"s or "1"s in them?
//
phoneNumber = phoneNumber.replace(/[^\d]/g,"");
// we'll replace the current list with our new, larger list after each
// iteration of the recursive function
//
for(i=0;i<phoneNumber.length;i++) a = addMnemonics(a,phoneNumber[i])
// do we need to match to a hashset of words? if so, we'd consider our
// array to be a list of possible answers. we'd need to iterate through
// and add every possible answer to a new list of answers, only if the
// possible answer exists in the hashset of words
//
return a;
}
function addMnemonics(a,s) {
var b = {
0: ["0"],
1: ["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"],
8: ["S","T","U"],
9: ["W","X","Y","Z"]
}[s], c=[];
// b is one of the arrays above, we'll return c = a x b
for(i=0;i<a.length;i++) for(j=0;j<b.length;j++) c.push(a[i] + b[j]);
return c;
}
JavaScript
- ic3b3rg November 22, 2013