---
id: 6989f7d2f0bac8cfe05a068d
title: Step 14
challengeType: 1
dashedName: step-14
---

# --description--

Finally, call `genParentheses` with `3` as its argument to generate all five valid combinations of three pairs of parentheses and log the result to the console.

With that, your breadth-first search workshop is complete.

# --hints--

You should log `genParentheses(3)`.

```js
assert.match(__helpers.removeJSComments(code), /console\.log\s*\(\s*genParentheses\s*\(\s*3\s*\)\s*\)/);
```

# --seed--

## --seed-contents--

```js
function genParentheses(pairs) {
  if (!Number.isInteger(pairs)) {
    return 'The number of pairs should be an integer';
  }
  if (pairs < 1) {
    return 'The number of pairs should be at least 1';
  }

  let queue = [['', 0, 0]];
  let result = [];

  while (queue.length > 0) {
    let [current, opensUsed, closesUsed] = queue.shift();

    if (current.length === 2 * pairs) {
      result.push(current);
    } else {
      if (opensUsed < pairs) {
        queue.push([current + '(', opensUsed + 1, closesUsed]);
      }
      if (closesUsed < opensUsed) {
        queue.push([current + ')', opensUsed, closesUsed + 1]);
      }
    }
  }
  return result;
}

console.log(genParentheses(2));
--fcc-editable-region--

--fcc-editable-region--
```

# --solutions--

```js
function genParentheses(pairs) {
  if (!Number.isInteger(pairs)) {
    return 'The number of pairs should be an integer';
  }
  if (pairs < 1) {
    return 'The number of pairs should be at least 1';
  }

  let queue = [['', 0, 0]];
  let result = [];

  while (queue.length > 0) {
    let [current, opensUsed, closesUsed] = queue.shift();

    if (current.length === 2 * pairs) {
      result.push(current);
    } else {
      if (opensUsed < pairs) {
        queue.push([current + '(', opensUsed + 1, closesUsed]);
      }
      if (closesUsed < opensUsed) {
        queue.push([current + ')', opensUsed, closesUsed + 1]);
      }
    }
  }
  return result;
}

console.log(genParentheses(2));
console.log(genParentheses(3));
```
