Skip to content

Commit 104e483

Browse files
authored
Merge pull request #1604 from acegautam/patch-2
JS solution to LC 682 | Baseball game
2 parents d803f93 + 8db5099 commit 104e483

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

Diff for: javascript/682-Baseball-Game.js

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/**
2+
* @param {string[]} operations
3+
* @return {number}
4+
*/
5+
var calPoints = function(operations) {
6+
let runningSum = 0;
7+
const stack = [];
8+
for(const o of operations) {
9+
if(o === 'C') {
10+
runningSum -= stack.pop();
11+
continue;
12+
}
13+
if(o === 'D') {
14+
const val = stack[stack.length - 1] * 2;
15+
stack.push(val);
16+
runningSum += val;
17+
continue;
18+
}
19+
if(o === '+') {
20+
const val = stack[stack.length - 1] + stack[stack.length - 2];
21+
stack.push(val);
22+
runningSum += val;
23+
continue;
24+
}
25+
stack.push(+o);
26+
runningSum += +o;
27+
}
28+
return runningSum;
29+
};

0 commit comments

Comments
 (0)