SHARE
TWEET

Untitled

a guest Jul 24th, 2019 65 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const digitMap = (digiStr) => {
  2.  
  3.   const digitMapping = {
  4.     2: ['a', 'b', 'c'],
  5.     3: ['d', 'e', 'f'],
  6.     4: ['g', 'h', 'i'],
  7.     5: ['j', 'k', 'l'],
  8.     6: ['m', 'n', 'o'],
  9.     7: ['p', 'q', 'r', 's'],
  10.     8: ['t', 'u', 'v'],
  11.     9: ['w', 'x', 'y', 'z']
  12.   }
  13.   const length = digiStr.length;
  14.   if (length === 1) return digitMapping[digiStr]; // in case of single digit strings
  15.   const indexes = []; // index addresses of charsets, in order
  16.   for (let i = 0; i < length; i++) {
  17.     indexes.push(0);
  18.   }
  19.   let nextI = length === 2 ? 0 : 1; // current spot of nextI in indexes
  20.   const possibleStrs = []; // for final answer of possible permutations
  21.   let str = '';
  22.   let endCount = 0; // if all addresses in indexes are at their end, then break while loop
  23.   while (true) {
  24.     console.log(indexes);
  25.     for (let i = 0; i < length; i++) {
  26.       str += digitMapping[digiStr[i]][indexes[i]];
  27.       if (indexes[i] === digitMapping[digiStr[i]].length - 1) {
  28.         endCount += 1;
  29.       }
  30.     }
  31.     possibleStrs.push(str);
  32.     str = '';
  33.     if (endCount === length) {
  34.       break;
  35.     }
  36.     endCount = 0;
  37.     // increment last item in indexes as long as it < length of corresponding inner char array
  38.     if (indexes[indexes.length - 1] < digitMapping[digiStr[digiStr.length - 1]].length - 1) {
  39.       indexes[indexes.length - 1] += 1;
  40.     }
  41.     else {
  42.       indexes[indexes.length - 1] = 0;
  43.       // increment nextI as long as < length of corresponding inner array
  44.       if (indexes[nextI] < digitMapping[digiStr[nextI]].length - 1) {
  45.         indexes[nextI] += 1;
  46.       }
  47.       else {
  48.       // otherwise shift position of nextI in indexes array
  49.         // if next position of nextI not end of indexes array
  50.           // shift nextI 1 spot forward in indexes
  51.         if (nextI + 1 < length - 1) {
  52.           nextI += 1;
  53.           indexes[nextI] += 1;
  54.         }
  55.         else {
  56.           // else reset all indexes to 0, starting from position 1
  57.           for (let i = 1; i < indexes.length; i++) {
  58.             indexes[i] = 0;
  59.           }
  60.           // increment first item in indexes
  61.           indexes[0] += 1;
  62.           nextI = 1; // place nextI at position 1
  63.         }
  64.       }
  65.     }
  66.   }
  67.   return possibleStrs;
  68. }
  69.  
  70. console.log(digitMap("2"));
  71. console.log(digitMap("23"));
  72. console.log(digitMap("234"));
  73. console.log(digitMap("2345"));
  74. console.log(digitMap("23456"));
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top