Given a string containing only 2-9, it can return all letter combinations indicated. Letters to numbers given map (FIG). Note that a letter does not correspond to any
Input: '23 '
Output: [ 'ad', 'ae', 'af', 'bd', 'be', 'bf', 'cd', 'ce', 'cf']
Law: 2 a count, makes up one, and continues to the next count, makes up 2, until the last
Code
Export default (STR) => { IF (! STR) return ; // establish a telephone number keypad mapping let strMap = [ '', ' ', 'abc', 'def', 'ghi', 'jkl', 'mno ',' PQRS ',' TUV ',' wxyz ' ]; // input a string of characters separated by a single array becomes, = 234> [2,3,4] the let NUM = str.split (' ' ); // character content after storage keyboard mapping, such as = 23 is> [ 'ABC', 'DEF] the let code = []; num.forEach (Item => { IF (strMap [Item]) { code.push(strMap[item]) } }) let comb = (arr) => { //The temporary variable to store the results of the first two combinations of the let TEMP = []; // outermost loop is traversing the first element, layer is a circle of a second element traversed for (I = 0 the let, IL = ARR [0] .length; I <IL; I ++ ) { for (J = 0 the let, ARR JL = [. 1] .length; J <JL; J ++ ) { temp.push ( `$ {ARR [ 0] [I ]} $ {ARR [. 1 ] [J]} `); } } arr.splice ( 0, 2 , TEMP); IF (arr.length>. 1 ) { COMB (ARR); } the else { return TEMP; } return ARR [0 ]; } return comb(code); }
Summarize knowledge
Array.prototype.splice