-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path[1차]-다트-게임&17682&.js
57 lines (52 loc) · 1.66 KB
/
[1차]-다트-게임&17682&.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
function solution(dartResult) {
const regex = /\d{1,2}[SDT]{1}[*|#]?/g;
let result = [];
for (const dart of dartResult.match(regex)) {
const game = [...dart.split(/([SDT]{1})/)];
const score = game[0];
let bonus = 1;
let option = 1;
if (game[1] === "S") bonus = 1;
if (game[1] === "D") bonus = 2;
if (game[1] === "T") bonus = 3;
if (game[2] === "*") {
if (result.length !== 0) result[result.length - 1] *= 2;
option = 2;
}
if (game[2] === "#") option = -1;
result.push(score ** bonus * option);
}
return result.reduce((a, b) => a + b);
}
//정답 2 - jaewon1676
function solution(dartResult) {
var answer = 0;
let score = 0;
let cnt = [];
for (let i = 0; i < dartResult.length; i++) {
//점수가 주어질때
if (!isNaN(dartResult[i])) {
// i가 1인 경우는 10점, 그외에는 점수
score = Number(dartResult[i - 1]) === 1 ? 10 : Number(dartResult[i]);
//보너스 S일때
} else if (dartResult[i] === "S") {
cnt.push(score);
//보너스 D일때
} else if (dartResult[i] === "D") {
cnt.push(Math.pow(score, 2));
//보너스 T일때
} else if (dartResult[i] === "T") {
cnt.push(Math.pow(score, 3));
//옵션 *일떄
} else if (dartResult[i] === "*") {
cnt[cnt.length - 2] = cnt[cnt.length - 2] * 2;
cnt[cnt.length - 1] = cnt[cnt.length - 1] * 2;
//옵션 #일때
} else if (dartResult[i] === "#") {
cnt[cnt.length - 1] = -1 * cnt[cnt.length - 1];
}
}
//3개의 점수 합산
answer = cnt.reduce((acc, cur) => acc + cur, 0);
return answer;
}