Created
February 19, 2012 23:18
-
-
Save marsch/1866429 to your computer and use it in GitHub Desktop.
find permutations to a telefphone number
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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