Skip to content

Commit 3654602

Browse files
committed
dart game / 중급
1 parent 8219e21 commit 3654602

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed

JustDevRae/Stack/dart_game.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
function solution(dartResult) {
2+
var answer = 0;
3+
const dartResultArray = dartResult.match(/\d+|[SDT]|\#|\*/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+
}

0 commit comments

Comments
 (0)