2018-10-10 18:03:03 -04:00
---
title: Balanced brackets
id: 594dc6c729e5700999302b45
challengeType: 5
2019-08-28 16:26:13 +03:00
forumTopicId: 302230
2018-10-10 18:03:03 -04:00
localeTitle: Сбалансированные кронштейны
---
## Description
2019-08-28 16:26:13 +03:00
< section id = 'description' >
< p > Определите, сбалансирована ли сгенерированная строка скобок; то есть, состоит ли он целиком из пар открывающих / закрывающих скобок (в этом порядке), ни одно из которых не выполняется. < / p > Примеры: < p class = "rosetta__paragraph" > (пусто) true < / p > < p class = "rosetta__paragraph" > < code > []< / code > true < / p > < p class = "rosetta__paragraph" > < code > ][< / code > false < / p > < p class = "rosetta__paragraph" > < code > [][]< / code > true < / p > < p class = "rosetta__paragraph" > < code > ][][< / code > false < / p > < p class = "rosetta__paragraph" > < code > []][[]< / code > false < / p > < p class = "rosetta__paragraph" > < code > [[[[]]]]< / code > true < / p >
< / section >
2018-10-10 18:03:03 -04:00
## Instructions
2019-08-28 16:26:13 +03:00
< section id = 'instructions' >
2018-10-10 18:03:03 -04:00
< / section >
## Tests
< section id = 'tests' >
```yml
tests:
2019-08-28 16:26:13 +03:00
- text: < code > isBalanced</ code > is a function.
testString: assert(typeof isBalanced === 'function');
- text: < code > isBalanced("[]")</ code > should return true.
testString: assert(isBalanced(testCases[0]));
- text: < code > isBalanced("]][[[][][][]][")</ code > should return false.
testString: assert(!isBalanced(testCases[1]));
- text: < code > isBalanced("[][[[[][][[[]]]]]]")</ code > should return true.
testString: assert(isBalanced(testCases[2]));
- text: < code > isBalanced("][")</ code > should return true.
testString: assert(!isBalanced(testCases[3]));
- text: < code > isBalanced("[[[]]]][[]")</ code > should return true.
testString: assert(!isBalanced(testCases[4]));
- text: < code > isBalanced("][[]")</ code > should return true.
testString: assert(!isBalanced(testCases[5]));
- text: < code > isBalanced("][[][]][[[]]")</ code > should return true.
testString: assert(!isBalanced(testCases[6]));
- text: < code > isBalanced("[[][]]][")</ code > should return true.
testString: assert(!isBalanced(testCases[7]));
- text: < code > isBalanced("[[[]]][[]]]][][[")</ code > should return true.
testString: assert(!isBalanced(testCases[8]));
- text: < code > isBalanced("[]][[]]][[[[][]]")</ code > should return true.
testString: assert(!isBalanced(testCases[9]));
- text: < code > isBalanced("][]][[][")</ code > should return true.
testString: assert(!isBalanced(testCases[10]));
- text: < code > isBalanced("[[]][[][]]")</ code > should return true.
testString: assert(isBalanced(testCases[11]));
- text: < code > isBalanced("[[]]")</ code > should return true.
testString: assert(isBalanced(testCases[12]));
- text: < code > isBalanced("]][]][[]][[[")</ code > should return true.
testString: assert(!isBalanced(testCases[13]));
- text: < code > isBalanced("][]][][[")</ code > should return true.
testString: assert(!isBalanced(testCases[14]));
- text: < code > isBalanced("][][")</ code > should return true.
testString: assert(!isBalanced(testCases[15]));
- text: < code > isBalanced("[[]]][][][[]][")</ code > should return true.
testString: assert(!isBalanced(testCases[16]));
- text: < code > isBalanced("")</ code > should return true.
testString: assert(isBalanced(testCases[17]));
2018-10-10 18:03:03 -04:00
```
< / section >
## Challenge Seed
< section id = 'challengeSeed' >
< div id = 'js-seed' >
```js
2019-08-28 16:26:13 +03:00
function isBalanced(str) {
2018-10-10 18:03:03 -04:00
// Good luck!
return true;
}
```
< / div >
2019-08-28 16:26:13 +03:00
### After Tests
2018-10-10 18:03:03 -04:00
< div id = 'js-teardown' >
```js
2019-08-28 16:26:13 +03:00
const testCases = [
'[]',
']][[[][][][]][',
'[][[[[][][[[]]]]]]',
'][',
'[[[]]]][[]',
'][[]',
'][[][]][[[]]',
'[[][]]][',
'[[[]]][[]]]][][[',
'[]][[]]][[[[][]]',
'][]][[][',
'[[]][[][]]',
'[[]]',
']][]][[]][[[',
'][]][][[',
'][][',
'[[]]][][][[]][',
''
];
2018-10-10 18:03:03 -04:00
```
< / div >
< / section >
## Solution
< section id = 'solution' >
```js
2019-08-28 16:26:13 +03:00
function isBalanced(str) {
if (str === '') return true;
let a = str;
let b;
do {
b = a;
a = a.replace(/\[\]/g, '');
} while (a !== b);
return !a;
}
2018-10-10 18:03:03 -04:00
```
2019-08-28 16:26:13 +03:00
2018-10-10 18:03:03 -04:00
< / section >