Skip to content

Instantly share code, notes, and snippets.

@arimado
Last active July 28, 2016 03:14
Show Gist options
  • Save arimado/8c94146a2a4d66ea81fda14caca2d0e5 to your computer and use it in GitHub Desktop.
Save arimado/8c94146a2a4d66ea81fda14caca2d0e5 to your computer and use it in GitHub Desktop.
var numPad = {
0: null,
1: null,
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"],
}
const telephoneWords = ( numsArr ) => {
let results = [];
numsArr.forEach((pressedNum, pressedNumIndex) => {
if (pressedNum < 2 || pressedNum > 9) {
throw "telephoneWords() - only accepts integers from 2-9"
}
let currentNumResult = [];
numPad[pressedNum].forEach((letter) => {
// go through all the next number
numsArr.forEach((pressedNumCheck, pressedNumCheckIndex) => {
if (pressedNumCheck !== pressedNum && pressedNumCheckIndex === pressedNumIndex + 1) {
numPad[pressedNumCheck].forEach((letterCheck) => {
currentNumResult.push(letter + letterCheck);
})
}
})
})
// APPEND NEXT MATCHES TO ROOT LETTERS
if ( results.length < 1 ) {
results = currentNumResult;
} else {
currentNumResult.forEach((letters) => {
results.forEach((rootLetters, index) => {
if(letters[0] === rootLetters[rootLetters.length - 1]) {
results[index] = rootLetters + letters[1];
}
})
})
}
})
return results.toString();
}
console.log(telephoneWords([2, 5]));
console.log(telephoneWords([2, 5, 8, 9]));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment