Advertisement
kurob

Untitled

Nov 28th, 2022 (edited)
801
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
JavaScript 1.63 KB | Writing | 0 0
  1. $(document).ready(function() {
  2.         // Driver Code
  3.         let input = "1123".split("");
  4.         let output = [];
  5.         printCombinations(input, 0, output, 0);
  6.     });
  7.  
  8.     function getAlpabet(num) {
  9.         const legend = '0ABCDEFGHIJKLMNOPQRSTUVWXYZ';
  10.         return legend[num];
  11.     }
  12.     function printCombinations(input,index,output,outLength) {
  13.         // no more digits left in input string
  14.         if (input.length == index) {
  15.             // print output string & return
  16.             var y = output.join("").split(" ").filter(function (x) {
  17.                 return x <= 26
  18.             })
  19.  
  20.             if (y.length != input.length) {
  21.                 if (y.join("")) {
  22.                     var label = ''
  23.                     var ou = output.join("").split(" ").map(function (x) {
  24.                         if (Number(x) > 0) {
  25.                             label += getAlpabet(Number(x))
  26.                         }
  27.                         return Number(x) > 0 ? getAlpabet(Number(x)) +' = '+ Number(x) + ', ' : ""
  28.                     }).join("")
  29.  
  30.                     console.log(label +'// '+ ou)
  31.                 }
  32.             }
  33.  
  34.             return;
  35.         }
  36.  
  37.         // place current digit in input string
  38.         output[outLength] = input[index];
  39.  
  40.         // separate next digit with a space
  41.         output[outLength + 1] = ' ';
  42.  
  43.  
  44.         printCombinations(input, index + 1, output, outLength + 2);
  45.  
  46.         // if next digit exists make a
  47.         // call without including space
  48.         if(input.length != index + 1) {
  49.             printCombinations(input, index + 1, output, outLength + 1);
  50.         }
  51.  
  52.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement