Baseball Game LeetCode solution in JavaScript

1
//Plus sign in the below algo confirms that the data type we are getting is integer. So, instead of adding it as a string, the data type will be  added as integer
2
var calPoints = function (ops) {
3
let stack = [];
4
for (let i = 0; i < ops.length; i++) {
5
if (ops[i] === "C") stack.pop();
6
else if (ops[i] === "D") stack.push(+stack[stack.length - 1] * 2);
7
//we have to take stack.length to get the element of stack. We cannot take i for getting the element of stack as i refers to ops and it wont give the index of previous element in stack
8
else if (ops[i] === "+")
9
stack.push(+stack[stack.length - 1] + +stack[stack.length - 2]);
10
else stack.push(+ops[i]);
11
}
12
let sum = 0;
13
for (let i = 0; i < stack.length; i++) {
14
sum = sum + stack[i];
15
}
16
return sum;
17
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0