so I have a problem. A string has either a number, DUP, POP, +, or -. if it's a number, the number is pushed to a stack. if DUP, the last number is duplicated and pushed. If POP, the last number is popped. if +, the last 2 numbers are added and popped and the sum is pushed to stack. If -, the last popped num - second popped num is pushed to stack. I'm having problem with my logic. I'm getting NaN when I did the case for DUP for some reason. For "3 DUP" i'm getting NaN.
function solution(S){
let arr = S.split(" ");
let stack = [];
for(let i = 0; i < arr.length; i++){
if(typeof parseInt(arr[i]) == "number") {
stack.push(parseInt(arr[i]));
continue;
} if (arr[i] == "DUP") {
let len = stack.length;
console.log(len)
let lastNum = stack[stack.length-1]
stack.push(parseInt(lastNum));
console.log(stack)
}
}
return stack.pop();
}