Confusing LeetCode

Input: ops = ["5","2","C","D","+"]
Output: 30
Explanation:
"5" - Add 5 to the record, record is now [5].
"2" - Add 2 to the record, record is now [5, 2].
"C" - Invalidate and remove the previous score, record is now [5].
"D" - Add 2 * 5 = 10 to the record, record is now [5, 10].
"+" - Add 5 + 10 = 15 to the record, record is now [5, 10, 15].
The total sum is 5 + 10 + 15 = 30.
/**
* @param {string[]} ops
* @return {number}
*/
var calPoints = function(ops) {
let score = [];
for(let i = 0; i < ops.length; i++){
if(ops[i] === "+")
score.push(ops[i-2] + ops[i-1]);
else if (ops[i] === "D")
score.push(ops[i-1] * 2);
else if (ops[i] === "C")
ops.splice(i-1, 2);
else
score.push(ops[i]);
}

console.log(score);

return score;
};

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store