title: Generate all valid combinations of N pairs of parentheses
body: <p>UPDATE (task detailed Explanation):
  We have a string consist of numbers 0 and 1, divided by operators |, ^ or &amp;. The task is to create all fully parenthesized expressions. So the final expressions should be divided into "2 parts" </p>
  
  <p>For example </p>
  
  <pre><code>0^1 -&gt; (0)^(1) but not extraneously: 0^1 -&gt; (((0))^(1))
  </code></pre>
  
  <p>Example for expression 1|0&amp;1:</p>
  
  <pre><code>(1)|((0)&amp;(1))
  ((1)|(0))&amp;(1)
  </code></pre>
  
  <p>As you can see both expressions above have left and write part:</p>
  
  <pre><code>left: (1); right: ((0)&amp;(1))
  left: ((1)|(0)); right: (1)
  </code></pre>
  
  <p>I tried the following code, but it does not work correctly (see output):</p>
  
  <pre><code>// expression has type string
  // result has type Array (ArrayList in Java)
  function setParens(expression, result) {
    if (expression.length === 1) return "(" + expression + ")";
  
  
    for (var i = 0; i &lt; expression.length; i++) {
      var c = expression[i];
      if (c === "|" || c === "^" || c === "&amp;") {
          var left  = expression.substring(0, i);
          var right = expression.substring(i + 1);
  
           leftParen = setParens(left, result);
           rightParen = setParens(right, result);
           var newExp = leftParen + c + rightParen;
           result.push(newExp);
      }
    }
    return expression;    
  }
  
  function test() {
    var r = [];
    setParens('1|0&amp;1', r);
    console.log(r);
  }
  
  test();
  
  code output: ["(0)&amp;(1)", "(0)|0&amp;1", "(1)|(0)", "1|0&amp;(1)"]
  </code></pre>