title: simplest code to check array possible combinations
body: <p>What's the simplest, library-free code for implementing get array possible combinations in javascript? I want to write</p>
  
  <pre><code>var preDefinedCombinations = [
      ['a', 'b', 'c'],
      ['a', 'c', 'd'],
      ['a'],
      ['b'],
      ['c']
  ];
  var selectedvalues = ['a', 'b', 'd'];
  getPossbileCombination(selectedvalues, preDefinedCombinations);
  </code></pre>
  
  <p>and get </p>
  
  <pre><code> ['a','b','c'],['a','c','d']
  </code></pre>
  
  <p>I tried below code but does'nt work as expected, it always give me missing term in case user select 'a', 'b' than alert shows him to select 'c' for valid combination. But if user selects 'a', 'b' &amp; ['d'] it does'nt give possible combination for selected values.</p>
  
  <pre><code>var preDefinedCombinations = [['a','b','c'], ['a','c','d'], ['a'], ['b'], ['c']];
  var selectedvalues = ['a','b','d'];
  
  function selectOption(value){
     var checkIndex = selectedvalues.indexof(value);
     if(checkIndex == -1){
         selectedvalues.push(value);
     }else{
         selectedvalues.splice(checkIndex, -1);
     }
  
  }
  
  function intersect_safe(a, b)
  {
   var ai=0, bi=0;
   var result = new Array();
  
   while( ai &lt; a.length &amp;&amp; bi &lt; b.length )
   {
      if      (a[ai] &lt; b[bi] ){ ai++; }
      else if (a[ai] &gt; b[bi] ){ bi++; }
      else /* they're equal */
      {
        result.push(a[ai]);
        ai++;
        bi++;
      }
   }
  
   return result;
  }
  
  function checkVaildCombination(){
     if(preDefinedCombinations.indexOf(selectedvalues) == -1){
         var missing_bits = [];
         for (var i = 0; i &lt; preDefinedCombinations.length; i++) {
             var missing_bit = intersect_safe(preDefinedCombinations[i], selectedvalues);
             if (missing_bit.length == selectedvalues.length) { // candidate for valid answer
                 missing_bit = [];
                 for (var j = 0; j &lt; preDefinedCombinations[i].length; j++) {
                     if (selectedvalues.indexOf(preDefinedCombinations[i][j]) == -1) { // actual missing bit
                         missing_bit.push(preDefinedCombinations[i][j]);
                     }
                 }
  
                 missing_bits.push(missing_bit);
             }
         }
  
         alert('Invalid Combination, if you select any of these you`ll get a valid combination:\n' + missing_bits.toString());
     }else{
         alert('Valid Combination');
     }
  }
  </code></pre>