## JavaScript || How To Find All Combinations Of Well-Formed Brackets Using Vanilla JavaScript The following is a module with functions which demonstrates how to find all combinations of well-formed brackets.

The task is to write a function Brackets(int n) that prints all combinations of well-formed brackets from 1…n. For example, Brackets(3), the output would be:

```() (()) ()() ((())) (()()) (())() ()(()) ()()()```

The number of possible combinations is the Catalan number of N pairs C(n).

1. Find All Well-Formed Brackets

The example below demonstrates the use of ‘Utils.brackets‘ to find all the well-formed bracket combinations.

``` 1. Find All Well-Formed Brackets JavaScript // Find All Well-Formed Brackets <script> (() => { let results = Utils.brackets(4); results.forEach((result) => { console.log(`Pair: \${result.pair}, Combination: \${result.combination}`); }); })(); </script> // expected output: /* Pair: 1, Combination: () Pair: 2, Combination: (()), ()() Pair: 3, Combination: ((())), (()()), (())(), ()(()), ()()() Pair: 4, Combination: (((()))), ((()())), ((())()), ((()))(), (()(())), (()()()), (()())(), (())(()), (())()(), ()((())), ()(()()), ()(())(), ()()(()), ()()()() */ 123456789101112131415161718 // Find All Well-Formed Brackets <script>(() => {    let results = Utils.brackets(4);        results.forEach((result) => {        console.log(`Pair: \${result.pair}, Combination: \${result.combination}`);    });})();</script> // expected output: /*    Pair: 1, Combination: ()     Pair: 2, Combination: (()), ()()     Pair: 3, Combination: ((())), (()()), (())(), ()(()), ()()()     Pair: 4, Combination: (((()))), ((()())), ((())()), ((()))(), (()(())), (()()()), (()())(), (())(()), (())()(), ()((())), ()(()()), ()(())(), ()()(()), ()()()()  */ ```

2. Utils Namespace

The following is the Utils.js Namespace. Include this in your project to start using!

``` Utils.js Namespace JavaScript // ============================================================================ // Author: Kenneth Perkins // Date: Oct 6, 2020 // Taken From: http://programmingnotes.org/ // File: Utils.js // Description: Javascript that handles general utility functions // ============================================================================ /** * NAMESPACE: Utils * USE: Handles general utility functions. */ var Utils = Utils || {}; (function(namespace) { 'use strict'; // Property to hold public variables and functions let exposed = namespace; /** * FUNCTION: brackets * USE: Returns all combinations of well-formed brackets from 1...n * @param pairs: The number of bracket combinations to generate. * @param open: Optional. The 'open bracket' symbol. * @param close: Optional. The 'close bracket' symbol. * @return: An array of bracket combination info. */ exposed.brackets = (pairs, open = '(', close = ')') => { let results = []; let symbols = { open: open, close: close, }; for (let pair = 1; pair <= pairs; ++pair) { results.push({ pair: pair, combination: buildBrackets('', 0, 0, pair, symbols), }); } return results; } let buildBrackets = (output, open, close, pair, symbols) => { if ((open === pair) && (close === pair)) { return output; } let result = ''; if (open < pair) { let openCombo = buildBrackets(output + symbols.open, open + 1, close, pair, symbols); if (openCombo.length > 0) { result += (result.length > 0 ? ', ' : '') + openCombo; } } if (close < open) { let closeCombo = buildBrackets(output + symbols.close, open, close + 1, pair, symbols); if (closeCombo.length > 0) { result += (result.length > 0 ? ', ' : '') + closeCombo; } } return result; } }(Utils)); // http://programmingnotes.org/ 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061 // ============================================================================//    Author: Kenneth Perkins//    Date:   Oct 6, 2020//    Taken From: http://programmingnotes.org///    File:  Utils.js//    Description: Javascript that handles general utility functions// ============================================================================/*** NAMESPACE: Utils* USE: Handles general utility functions.*/var Utils = Utils || {};(function(namespace) {    'use strict';     // Property to hold public variables and functions    let exposed = namespace;     /**    * FUNCTION: brackets    * USE: Returns all combinations of well-formed brackets from 1...n    * @param pairs: The number of bracket combinations to generate.    * @param open: Optional. The 'open bracket' symbol.    * @param close: Optional. The 'close bracket' symbol.    * @return: An array of bracket combination info.    */    exposed.brackets = (pairs, open = '(', close = ')') => {        let results = [];        let symbols = {            open: open,            close: close,        };        for (let pair = 1; pair <= pairs; ++pair) {            results.push({                pair: pair,                combination: buildBrackets('', 0, 0, pair, symbols),            });        }        return results;    }     let buildBrackets = (output, open, close, pair, symbols) => {        if ((open === pair) && (close === pair)) {            return output;        }        let result = '';        if (open < pair) {            let openCombo = buildBrackets(output + symbols.open, open + 1, close, pair, symbols);            if (openCombo.length > 0) {                result += (result.length > 0 ? ', ' : '') + openCombo;            }        }        if (close < open) {            let closeCombo = buildBrackets(output + symbols.close, open, close + 1, pair, symbols);            if (closeCombo.length > 0) {                result += (result.length > 0 ? ', ' : '') + closeCombo;            }        }        return result;    }}(Utils)); // http://programmingnotes.org/ ```

QUICK NOTES:
The highlighted lines are sections of interest to look out for.

The code is heavily commented, so no further insight is necessary. If you have any questions, feel free to leave a comment below.

## JavaScript || How To Calculate Nth Catalan Number Using Vanilla JavaScript In combinatorial mathematics, a Catalan number forms a sequence of natural numbers that occur in various counting problems. It can be used to determine how many different ways to split something up.

The following is a module with functions which demonstrates how to calculate nth Catalan number.

1. Calculate Nth Catalan Number

The example below demonstrates the use of ‘Utils.catalan‘ to calculate nth Catalan number.

``` 1. Calculate Nth Catalan Number JavaScript // Calculate Nth Catalan Number <script> (() => { for (let number = 0; number <= 10; ++number) { console.log(`Number: \${number}, Catalan: \${Utils.catalan(number)}`); } })(); </script> // expected output: /* Number: 0, Catalan: 1 Number: 1, Catalan: 1 Number: 2, Catalan: 2 Number: 3, Catalan: 5 Number: 4, Catalan: 14 Number: 5, Catalan: 42 Number: 6, Catalan: 132 Number: 7, Catalan: 429 Number: 8, Catalan: 1430 Number: 9, Catalan: 4862 Number: 10, Catalan: 16796 */ 123456789101112131415161718192021222324 // Calculate Nth Catalan Number <script>(() => {        for (let number = 0; number <= 10; ++number) {        console.log(`Number: \${number}, Catalan: \${Utils.catalan(number)}`);    }})();</script> // expected output: /*    Number: 0, Catalan: 1      Number: 1, Catalan: 1      Number: 2, Catalan: 2      Number: 3, Catalan: 5      Number: 4, Catalan: 14      Number: 5, Catalan: 42      Number: 6, Catalan: 132      Number: 7, Catalan: 429      Number: 8, Catalan: 1430      Number: 9, Catalan: 4862      Number: 10, Catalan: 16796  */ ```

2. Utils Namespace

The following is the Utils.js Namespace. Include this in your project to start using!

``` Utils.js Namespace JavaScript // ============================================================================ // Author: Kenneth Perkins // Date: Oct 6, 2020 // Taken From: http://programmingnotes.org/ // File: Utils.js // Description: Javascript that handles general utility functions // ============================================================================ /** * NAMESPACE: Utils * USE: Handles general utility functions. */ var Utils = Utils || {}; (function(namespace) { 'use strict'; // Property to hold public variables and functions let exposed = namespace; /** * FUNCTION: catalan * USE: Calculates the catalan number for the number in question * @param n: The number to calculate the catalan number. * @return: The catalan number for the number in question. */ exposed.catalan = (n) => { n = Math.max(0, n); let result = Array(n + 1); result = result = 1; for (let x = 2; x <= n; ++x) { result[x] = 0; for (let y = 0; y < x; ++y) { result[x] += result[y] * result[x - y - 1]; } } return result[n]; } }(Utils)); // http://programmingnotes.org/ 12345678910111213141516171819202122232425262728293031323334353637 // ============================================================================//    Author: Kenneth Perkins//    Date:   Oct 6, 2020//    Taken From: http://programmingnotes.org///    File:  Utils.js//    Description: Javascript that handles general utility functions// ============================================================================/*** NAMESPACE: Utils* USE: Handles general utility functions.*/var Utils = Utils || {};(function(namespace) {    'use strict';     // Property to hold public variables and functions    let exposed = namespace;     /**    * FUNCTION: catalan    * USE: Calculates the catalan number for the number in question    * @param n: The number to calculate the catalan number.    * @return: The catalan number for the number in question.    */    exposed.catalan = (n) => {        n = Math.max(0, n);        let result = Array(n + 1);        result = result = 1;        for (let x = 2; x <= n; ++x) {            result[x] = 0;            for (let y = 0; y < x; ++y) {                result[x] += result[y] * result[x - y - 1];            }        }        return result[n];    }}(Utils)); // http://programmingnotes.org/ ```

QUICK NOTES:
The highlighted lines are sections of interest to look out for.

The code is heavily commented, so no further insight is necessary. If you have any questions, feel free to leave a comment below.