2018-10-04 14:37:37 +01:00
---
title: Balanced brackets
id: 594dc6c729e5700999302b45
challengeType: 5
2019-08-05 09:17:33 -07:00
forumTopicId: 302230
2018-10-04 14:37:37 +01:00
---
## Description
< section id = 'description' >
2019-02-25 13:36:09 +09:00
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.
2019-03-01 17:10:50 +09:00
< h4 > < strong > Examples:< / strong > < / h4 >
| Input | Output |
| --- | --- |
| < code > []< / code > | true |
| < code > ][< / code > | false |
| < code > [][]< / code > | true |
| < code > ][][< / code > | false |
| < code > []][[]< / code > | false |
| < code > [[[[]]]]< / code > | true |
2018-10-04 14:37:37 +01:00
< / section >
## Instructions
< section id = 'instructions' >
< / section >
## Tests
< section id = 'tests' >
```yml
tests:
2019-11-20 07:01:31 -08:00
- text: < code > isBalanced</ code > should be a function.
2019-07-26 05:24:52 -07:00
testString: assert(typeof isBalanced === 'function');
2018-10-20 21:02:47 +03:00
- text: < code > isBalanced("[]")</ code > should return true.
2019-07-26 05:24:52 -07:00
testString: assert(isBalanced(testCases[0]));
2018-10-20 21:02:47 +03:00
- text: < code > isBalanced("]][[[][][][]][")</ code > should return false.
2019-07-26 05:24:52 -07:00
testString: assert(!isBalanced(testCases[1]));
2018-10-20 21:02:47 +03:00
- text: < code > isBalanced("[][[[[][][[[]]]]]]")</ code > should return true.
2019-07-26 05:24:52 -07:00
testString: assert(isBalanced(testCases[2]));
2019-09-25 04:39:11 -05:00
- text: < code > isBalanced("][")</ code > should return false.
2019-07-26 05:24:52 -07:00
testString: assert(!isBalanced(testCases[3]));
2019-09-25 04:39:11 -05:00
- text: < code > isBalanced("[[[]]]][[]")</ code > should return false.
2019-07-26 05:24:52 -07:00
testString: assert(!isBalanced(testCases[4]));
2019-09-25 04:39:11 -05:00
- text: < code > isBalanced("][[]")</ code > should return false.
2019-07-26 05:24:52 -07:00
testString: assert(!isBalanced(testCases[5]));
2019-09-25 04:39:11 -05:00
- text: < code > isBalanced("][[][]][[[]]")</ code > should return false.
2019-07-26 05:24:52 -07:00
testString: assert(!isBalanced(testCases[6]));
2019-09-25 04:39:11 -05:00
- text: < code > isBalanced("[[][]]][")</ code > should return false.
2019-07-26 05:24:52 -07:00
testString: assert(!isBalanced(testCases[7]));
2019-09-25 04:39:11 -05:00
- text: < code > isBalanced("[[[]]][[]]]][][[")</ code > should return false.
2019-07-26 05:24:52 -07:00
testString: assert(!isBalanced(testCases[8]));
2019-09-25 04:39:11 -05:00
- text: < code > isBalanced("[]][[]]][[[[][]]")</ code > should return false.
2019-07-26 05:24:52 -07:00
testString: assert(!isBalanced(testCases[9]));
2019-09-25 04:39:11 -05:00
- text: < code > isBalanced("][]][[][")</ code > should return false.
2019-07-26 05:24:52 -07:00
testString: assert(!isBalanced(testCases[10]));
2018-10-20 21:02:47 +03:00
- text: < code > isBalanced("[[]][[][]]")</ code > should return true.
2019-07-26 05:24:52 -07:00
testString: assert(isBalanced(testCases[11]));
2018-10-20 21:02:47 +03:00
- text: < code > isBalanced("[[]]")</ code > should return true.
2019-07-26 05:24:52 -07:00
testString: assert(isBalanced(testCases[12]));
2019-09-25 04:39:11 -05:00
- text: < code > isBalanced("]][]][[]][[[")</ code > should return false.
2019-07-26 05:24:52 -07:00
testString: assert(!isBalanced(testCases[13]));
2019-09-25 04:39:11 -05:00
- text: < code > isBalanced("][]][][[")</ code > should return false.
2019-07-26 05:24:52 -07:00
testString: assert(!isBalanced(testCases[14]));
2019-09-25 04:39:11 -05:00
- text: < code > isBalanced("][][")</ code > should return false.
2019-07-26 05:24:52 -07:00
testString: assert(!isBalanced(testCases[15]));
2019-09-25 04:39:11 -05:00
- text: < code > isBalanced("[]]]")</ code > should return false.
2019-07-26 05:24:52 -07:00
testString: assert(!isBalanced(testCases[16]));
2018-10-04 14:37:37 +01:00
- text: < code > isBalanced("")</ code > should return true.
2019-07-26 05:24:52 -07:00
testString: assert(isBalanced(testCases[17]));
2018-10-04 14:37:37 +01:00
```
< / section >
## Challenge Seed
< section id = 'challengeSeed' >
< div id = 'js-seed' >
```js
2019-02-26 17:07:07 +09:00
function isBalanced(str) {
2020-09-15 09:57:40 -07:00
2018-10-04 14:37:37 +01:00
return true;
}
```
< / div >
### After Test
< div id = 'js-teardown' >
```js
2018-10-20 21:02:47 +03:00
const testCases = [
'[]',
']][[[][][][]][',
'[][[[[][][[[]]]]]]',
'][',
'[[[]]]][[]',
'][[]',
'][[][]][[[]]',
'[[][]]][',
'[[[]]][[]]]][][[',
'[]][[]]][[[[][]]',
'][]][[][',
'[[]][[][]]',
'[[]]',
']][]][[]][[[',
'][]][][[',
'][][',
2019-09-25 04:39:11 -05:00
'[]]]',
2018-10-20 21:02:47 +03:00
''
];
2018-10-04 14:37:37 +01:00
```
< / div >
< / section >
## Solution
< section id = 'solution' >
```js
2019-02-26 17:07:07 +09:00
function isBalanced(str) {
2018-10-20 21:02:47 +03:00
if (str === '') return true;
2018-10-04 14:37:37 +01:00
let a = str;
let b;
do {
b = a;
2018-10-20 21:02:47 +03:00
a = a.replace(/\[\]/g, '');
2018-10-04 14:37:37 +01:00
} while (a !== b);
return !a;
}
```
< / section >