Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 58a606d

Browse files
Add 220411 [1차]-다트-게임.js 1개의 풀이 추가
1 parent 347d558 commit 58a606d

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

‎level-1/[1차]-다트-게임.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,38 @@ function solution(dartResult) {
2323
}
2424
answer = result.reduce((a, b) => a + b)
2525
return answer;
26+
}
27+
28+
//정답 2 - jaewon1676
29+
function solution(dartResult) {
30+
var answer = 0;
31+
let score = 0;
32+
let cnt = [];
33+
34+
for (let i = 0; i < dartResult.length; i++) {
35+
//점수가 주어질때
36+
if (!isNaN(dartResult[i])) {
37+
// i가 1인 경우는 10점, 그외에는 점수
38+
score = Number(dartResult[i - 1]) === 1 ? 10 : Number(dartResult[i]);
39+
//보너스 S일때
40+
} else if (dartResult[i] === "S") {
41+
cnt.push(score);
42+
//보너스 D일때
43+
} else if (dartResult[i] === "D") {
44+
cnt.push(Math.pow(score, 2));
45+
//보너스 T일때
46+
} else if (dartResult[i] === "T") {
47+
cnt.push(Math.pow(score, 3));
48+
//옵션 *일떄
49+
} else if (dartResult[i] === "*") {
50+
cnt[cnt.length - 2] = cnt[cnt.length - 2] * 2;
51+
cnt[cnt.length - 1] = cnt[cnt.length - 1] * 2;
52+
//옵션 #일때
53+
} else if (dartResult[i] === "#") {
54+
cnt[cnt.length - 1] = -1 * cnt[cnt.length - 1];
55+
}
56+
}
57+
//3개의 점수 합산
58+
answer = cnt.reduce((acc, cur) => acc + cur, 0);
59+
return answer;
2660
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /