Skip to content

Instantly share code, notes, and snippets.

@marsch
Created February 19, 2012 23:18
Show Gist options
  • Save marsch/1866429 to your computer and use it in GitHub Desktop.
Save marsch/1866429 to your computer and use it in GitHub Desktop.
find permutations to a telefphone number
var map = {0: ' ', 1: '.', 2: 'abc', 3: 'def', 4: 'ghi', 5: 'jkl', 6: 'mno', 7: 'pqrs', 8:'tuv', 9:'wxyz'};
var number = 4878366;
var out = '';
var endl = '\n';
function getPerms(num, str, map) {
if (num == 0) {
out += str + endl;
} else {
var curDigit = num % 10;
var newNum = Math.floor(num / 10);
var s = map[curDigit];
for (var i=0; i< s.length; i++) {
getPerms(newNum, s.charAt(i) + str, map);
}
}
}
getPerms(number, "", map);
console.log(out);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment