File tree Expand file tree Collapse file tree 1 file changed +41
-0
lines changed
Expand file tree Collapse file tree 1 file changed +41
-0
lines changed Original file line number Diff line number Diff line change 1+ function solution ( dartResult ) {
2+ var answer = 0 ;
3+ const dartResultArray = dartResult . match ( / \d + | [ S D T ] | \# | \* / g) ;
4+ const stack = [ ] ;
5+ let value ;
6+
7+ for ( str of dartResultArray ) {
8+ if ( str === "S" ) {
9+ value = stack . pop ( ) ;
10+ stack . push ( value ) ;
11+ } else if ( str === "D" ) {
12+ value = stack . pop ( ) ;
13+ stack . push ( value * value ) ;
14+ } else if ( str === "T" ) {
15+ value = stack . pop ( ) ;
16+ stack . push ( value * value * value ) ;
17+ } else if ( str === "*" ) {
18+ let firtPopValue = stack . pop ( ) ;
19+ firtPopValue *= 2 ;
20+ let secondPopValue = stack . pop ( ) ;
21+ if ( secondPopValue === undefined ) {
22+ stack . push ( firtPopValue ) ;
23+ } else {
24+ secondPopValue *= 2 ;
25+ stack . push ( secondPopValue ) ;
26+ stack . push ( firtPopValue ) ;
27+ }
28+ } else if ( str === "#" ) {
29+ value = stack . pop ( ) ;
30+ stack . push ( value * - 1 ) ;
31+ } else {
32+ stack . push ( Number ( str ) ) ;
33+ }
34+ }
35+
36+ for ( element of stack ) {
37+ answer += element ;
38+ }
39+
40+ return answer ;
41+ }
You can’t perform that action at this time.
0 commit comments