File tree Expand file tree Collapse file tree 1 file changed +8
-6
lines changed Expand file tree Collapse file tree 1 file changed +8
-6
lines changed Original file line number Diff line number Diff line change @@ -11,21 +11,23 @@ export function parenthesesChecker(symbols) {
11
11
let top ;
12
12
13
13
while ( index < symbols . length && balanced ) {
14
- symbol = symbols . charAt ( index ) ;
14
+ symbol = symbols [ index ] ;
15
15
if ( opens . indexOf ( symbol ) >= 0 ) {
16
16
stack . push ( symbol ) ;
17
17
} else if ( stack . isEmpty ( ) ) {
18
18
balanced = false ;
19
19
} 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
+ }
21
26
if ( ! ( opens . indexOf ( top ) === closers . indexOf ( symbol ) ) ) {
22
27
balanced = false ;
23
28
}
24
29
}
25
30
index ++ ;
26
31
}
27
- if ( balanced && stack . isEmpty ( ) ) {
28
- return true ;
29
- }
30
- return false ;
32
+ return balanced && stack . isEmpty ( ) ;
31
33
}
You can’t perform that action at this time.
0 commit comments