Double '+' signs messing with !NaN in JS

Working a evaluator for Polish Notation and I was adding a way to distinguish if the string I got was a number or not using the isNaN function. This works fine, until you add + + to the string.

function cuttingString(list) {
    let adjustArr = list.split(' ')
    let topper = []
    for (let i = 0; i < adjustArr.length; i++) {
        if (!isNaN(adjustArr[i])) {
            topper.push(adjustArr[i])
            console.log(topper)
        } else {
            let j = topper.pop()
            let k = topper.pop()

            if (adjustArr[i] === '+') {
                topper.push(parseInt(j) + parseInt(k))
            }
        }
    }
}

console.log(cuttingString('* 1 2 30 +'))

works as expected with '* 1 2 30 +' it outputs [1,2,30] However, when I start moving around the operators, is when I get a NaN in the beginning of my arrays [NaN, 1,2,30] which is incredibly frustrating. Any ideas on how to fix this problem or other work arounds?

64 thoughts on “Double '+' signs messing with !NaN in JS”

Leave a Comment