Skip to content

Commit 7dadd5d

Browse files
committed
Dart Game / 중급
1 parent a560deb commit 7dadd5d

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
function solution(dartResult) {
2+
const stack = [];
3+
let i = 0;
4+
5+
while (i < dartResult.length) {
6+
const isTen = dartResult[i] === "1" && dartResult[i + 1] === "0";
7+
let score = isTen ? 10 : parseInt(dartResult[i]);
8+
i += isTen ? 2 : 1;
9+
10+
const bonus = dartResult[i++];
11+
score **= bonus === "S" ? 1 : bonus === "D" ? 2 : 3;
12+
13+
if (dartResult[i] === "*" || dartResult[i] === "#") {
14+
score *= dartResult[i] === "*" ? 2 : -1;
15+
if (dartResult[i++] === "*" && stack.length) {
16+
stack[stack.length - 1] *= 2;
17+
}
18+
}
19+
20+
stack.push(score);
21+
}
22+
23+
return stack.reduce((sum, cur) => sum + cur, 0);
24+
}

0 commit comments

Comments
 (0)