-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
37 lines (30 loc) · 819 Bytes
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
function validateBrackets(string) {
const array1 = string.split('');
let counter1 = 0;
let counter2 = 0;
let direction = 'open';
let isCountersOnStepEqual = true;
array1.forEach(el => {
switch(true) {
case direction === 'open' && el === '(':
counter1++;
break;
case direction === 'open' && el === ')':
direction = 'close';
counter2++;
break;
case direction === 'close' && el === ')':
counter2++;
break;
case direction === 'close' && el === '(':
direction = 'open';
counter1++;
break;
}
if (counter2 > counter1) {
isCountersOnStepEqual = false;
}
})
return counter1 === counter2 && isCountersOnStepEqual;
}
module.exports.validateBrackets = validateBrackets;