-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbaseball.js
39 lines (30 loc) · 840 Bytes
/
baseball.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
var calPoints = function(operations) {
let result = [];
let sum=0
let prev = -1;
let maxPrev = 0;
let prevprev = 0;
for (let i = 0; i < operations.length; i++) {
let current = operations[i];
if (current === '+') {
maxPrev = prev + prevprev;
} else if (current === 'D') {
maxPrev = prev * 2;
} else if (current === 'C') {
result.pop();
prev = result[result.length - 1] || 0;
continue;
} else {
maxPrev = parseInt(current);
}
result.push(maxPrev);
prevprev = prev;
prev = maxPrev;
}
const data = result.map((item)=>parseInt(item))
for(let i=0; i<data.length; i++){
sum += data[i]
}
return sum
};
console.log(calPoints(["5","2","C","D","+"]));