// Source : https://oj.leetcode.com/problems/letter-combinations-of-a-phone-number/
// Author : Dean Shi
// Date   : 2015-06-11

/**********************************************************************************
 *
 * Given a digit string, return all possible letter combinations that the number could represent.
 *
 * A mapping of digit to letters (just like on the telephone buttons) is given below.
 *
 * Input:Digit string "23"
 * Output: ["ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"].
 *
 * Note:
 * Although the above answer is in lexicographical order, your answer could be in any order you want.
 *
 *
 **********************************************************************************/

/**
 * @param {string} digits
 * @return {string[]}
 */
var letterCombinations = function(digits) {
    return f([], digits);
};

var map = {
    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']
};

function f(arr, digits) {
    if (!digits.length) {
        return arr;
    }
    var num = digits[0]; // first char
    var tmp = arr.length ? [] : map[num];

    arr.forEach(function(v) {
        map[num].forEach(function(m) {
            tmp.push(v + m);
        });
    });
    return f(tmp, digits.substring(1));
}


// Test case
console.log(letterCombinations('23').toString() === 'ad,ae,af,bd,be,bf,cd,ce,cf'); // true

@@ -807,46 +807,46 @@
     
-return f(%5B%5D, digits);%0A%7D;%0A%0Avar m
+let result%0A    const digitM
 ap = %7B%0A
+    
     
@@ -861,24 +861,28 @@
  'b', 'c'%5D,%0A
+    
     3: %5B'd',
@@ -889,24 +889,28 @@
  'e', 'f'%5D,%0A
+    
     4: %5B'g',
@@ -925,16 +925,20 @@
 '%5D,%0A    
+    
 5: %5B'j',
@@ -945,24 +945,28 @@
  'k', 'l'%5D,%0A
+    
     6: %5B'm',
@@ -973,24 +973,28 @@
  'n', 'o'%5D,%0A
+    
     7: %5B'p',
@@ -1014,16 +1014,20 @@
 '%5D,%0A    
+    
 8: %5B't',
@@ -1042,16 +1042,20 @@
 '%5D,%0A    
+    
 9: %5B'w',
@@ -1074,290 +1074,171 @@
 z'%5D%0A
-%7D;%0A%0Afunction f(arr, digits) %7B%0A    if (!digits.length) %7B%0A        return arr;%0A    %7D%0A    var num = digits%5B0%5D; // first char%0A    var tmp = arr.length ? %5B%5D : map%5Bnum%5D;%0A%0A    arr.forEach(function(v) %7B%0A        map%5Bnum%5D.forEach(function(m) %7B%0A            tmp
+    %7D%0A    %0A    return %5B...digits.substring(1)%5D.reduce((arr, d) =%3E %7B%0A        result = %5B%5D%0A        arr.forEach(a =%3E digitMap%5Bd%5D.forEach(b =%3E result
 .push(
-v + m);%0A        %7D);
+a + b)))
 %0A    
-%7D);%0A
     
@@ -1248,38 +1248,51 @@
 urn 
-f(tmp, digits.substring(1));
+result%0A    %7D, digitMap%5Bdigits%5B0%5D%5D %7C%7C %5B%5D)
 %0A%7D
+;
 %0A%0A%0A/

Update LetterCombinationsOfAPhoneNumber.js