Skip to content

Commit

Permalink
Complete telephoneWords.js using recursion
Browse files Browse the repository at this point in the history
  • Loading branch information
fay-jai committed Jan 2, 2015
1 parent b6f89cf commit 42edbe5
Showing 1 changed file with 17 additions and 2 deletions.
19 changes: 17 additions & 2 deletions telephoneWords/telephoneWords.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,21 @@ var phoneDigitsToLetters = {
};


var telephoneWords = function(digitString) {
// TODO: return every combination that can be spelled on a phone with these digits
var telephoneWords = function (digitString) {
var len = digitString.length;
var result = [];
var recur;

if ( len <= 0 ) return [];
if ( len === 1 ) return phoneDigitsToLetters[ digitString ].split('');

recur = telephoneWords( digitString.slice(1) );

phoneDigitsToLetters[ digitString[0] ].split('').forEach(function (letter) {
recur.forEach(function (array) {
result.push( Array.prototype.concat( letter, array ) );
});
});

return result;
};

0 comments on commit 42edbe5

Please sign in to comment.