Skip to content

Commit 7309191

Browse files
author
Christian Bender
committedApr 8, 2018
improved the algorithm
1 parent 90aaa3c commit 7309191

File tree

1 file changed

+8
-6
lines changed

1 file changed

+8
-6
lines changed
 

‎src/js/others/balanced-symbols.js

+8-6
Original file line numberDiff line numberDiff line change
@@ -11,21 +11,23 @@ export function parenthesesChecker(symbols) {
1111
let top;
1212

1313
while (index < symbols.length && balanced) {
14-
symbol = symbols.charAt(index);
14+
symbol = symbols[index];
1515
if (opens.indexOf(symbol) >= 0) {
1616
stack.push(symbol);
1717
} else if (stack.isEmpty()) {
1818
balanced = false;
1919
} else {
20-
top = stack.pop();
20+
// makes sure the stack isn't empty.
21+
if (!stack.isEmpty()) {
22+
top = stack.pop();
23+
} else { // error case
24+
balanced = false;
25+
}
2126
if (!(opens.indexOf(top) === closers.indexOf(symbol))) {
2227
balanced = false;
2328
}
2429
}
2530
index++;
2631
}
27-
if (balanced && stack.isEmpty()) {
28-
return true;
29-
}
30-
return false;
32+
return balanced && stack.isEmpty();
3133
}

0 commit comments

Comments
 (0)