Skip to content

Instantly share code, notes, and snippets.

@huttj
Created May 27, 2016 22:29
Show Gist options
  • Save huttj/28339af0b1c978efc24e98e1b0ce146b to your computer and use it in GitHub Desktop.
Save huttj/28339af0b1c978efc24e98e1b0ce146b to your computer and use it in GitHub Desktop.
var lookups = {
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', 's'],
8: ['t', 'u', 'v'],
9: ['w', 'x', 'y', 'z'],
0: [' '],
};
var Tel = {
getCharacters: function(n) {
return lookups[n] || [];
}
};
console.log(JSON.stringify(t9('5674'), null, 2));
function t9(numbers) {
return t9helper([], numbers.split(''));
}
function t9helper(seed, numbers) {
if (numbers.length === 0) {
return seed.join('');
}
var letters = Tel.getCharacters(numbers[0]);
return letters.map(l => {
return t9helper(seed.concat(l), numbers.slice(1));
}).reduce((acc,n) => acc.concat(n), []);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment