--- title: Balanced brackets id: 594dc6c729e5700999302b45 challengeType: 5 forumTopicId: 302230 --- ## Description
Determine whether a generated string of brackets is balanced; that is, whether it consists entirely of pairs of opening/closing brackets (in that order), none of which mis-nest.

Examples:

| Input | Output | | --- | --- | | [] | true | | ][ | false | | [][] | true | | ][][ | false | | []][[] | false | | [[[[]]]] | true |
## Instructions
## Tests
```yml tests: - text: isBalanced should be a function. testString: assert(typeof isBalanced === 'function'); - text: isBalanced("[]") should return true. testString: assert(isBalanced(testCases[0])); - text: isBalanced("]][[[][][][]][") should return false. testString: assert(!isBalanced(testCases[1])); - text: isBalanced("[][[[[][][[[]]]]]]") should return true. testString: assert(isBalanced(testCases[2])); - text: isBalanced("][") should return false. testString: assert(!isBalanced(testCases[3])); - text: isBalanced("[[[]]]][[]") should return false. testString: assert(!isBalanced(testCases[4])); - text: isBalanced("][[]") should return false. testString: assert(!isBalanced(testCases[5])); - text: isBalanced("][[][]][[[]]") should return false. testString: assert(!isBalanced(testCases[6])); - text: isBalanced("[[][]]][") should return false. testString: assert(!isBalanced(testCases[7])); - text: isBalanced("[[[]]][[]]]][][[") should return false. testString: assert(!isBalanced(testCases[8])); - text: isBalanced("[]][[]]][[[[][]]") should return false. testString: assert(!isBalanced(testCases[9])); - text: isBalanced("][]][[][") should return false. testString: assert(!isBalanced(testCases[10])); - text: isBalanced("[[]][[][]]") should return true. testString: assert(isBalanced(testCases[11])); - text: isBalanced("[[]]") should return true. testString: assert(isBalanced(testCases[12])); - text: isBalanced("]][]][[]][[[") should return false. testString: assert(!isBalanced(testCases[13])); - text: isBalanced("][]][][[") should return false. testString: assert(!isBalanced(testCases[14])); - text: isBalanced("][][") should return false. testString: assert(!isBalanced(testCases[15])); - text: isBalanced("[]]]") should return false. testString: assert(!isBalanced(testCases[16])); - text: isBalanced("") should return true. testString: assert(isBalanced(testCases[17])); ```
## Challenge Seed
```js function isBalanced(str) { return true; } ```
### After Test
```js const testCases = [ '[]', ']][[[][][][]][', '[][[[[][][[[]]]]]]', '][', '[[[]]]][[]', '][[]', '][[][]][[[]]', '[[][]]][', '[[[]]][[]]]][][[', '[]][[]]][[[[][]]', '][]][[][', '[[]][[][]]', '[[]]', ']][]][[]][[[', '][]][][[', '][][', '[]]]', '' ]; ```
## Solution
```js function isBalanced(str) { if (str === '') return true; let a = str; let b; do { b = a; a = a.replace(/\[\]/g, ''); } while (a !== b); return !a; } ```