-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathdart_game.js
More file actions
41 lines (38 loc) · 993 Bytes
/
dart_game.js
File metadata and controls
41 lines (38 loc) · 993 Bytes
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
function solution(dartResult) {
var answer = 0;
const dartResultArray = dartResult.match(/\d+|[SDT]|\#|\*/g);
const stack = [];
let value;
for (str of dartResultArray) {
if (str === "S") {
value = stack.pop();
stack.push(value);
} else if (str === "D") {
value = stack.pop();
stack.push(value * value);
} else if (str === "T") {
value = stack.pop();
stack.push(value * value * value);
} else if (str === "*") {
let firtPopValue = stack.pop();
firtPopValue *= 2;
let secondPopValue = stack.pop();
if (secondPopValue === undefined) {
stack.push(firtPopValue);
} else {
secondPopValue *= 2;
stack.push(secondPopValue);
stack.push(firtPopValue);
}
} else if (str === "#") {
value = stack.pop();
stack.push(value * -1);
} else {
stack.push(Number(str));
}
}
for (element of stack) {
answer += element;
}
return answer;
}