为什么 for 循环会跳过部分字符串?

Why is for loop skipping part of string?

我正在创建一个遵循操作顺序的计算器应用程序。我的问题是 else if 在第 10 行检查括号。我想让它做什么,它检查一个左括号,运行 递归地通过我的计算器方法检查里面的内容,将它推入我解析的字符串数组以再次计算,从我的字符串中删除括号,然后再次启动我在 0 将 for 循环移动到我的字符串前面。出于某种原因,它会跳到第二个括号并 运行ning 两次。这是我的代码:

Calculator("(3*2)/(2+1)+4");
//Should get 3
function parseCalculationString(expression){
    let parsedString = [];
    for(i = 0; i<expression.length; i++){
        if(expression[i]!=" "){
            if (expression[i]>='0' && expression[i]<='9'){
                parsedString.push(parseFloat(expression[i]));
            }
            else if(expression[i] == '('){
                //let parentheses = Calculator(expression.split('(').pop().split(')')[0]);
                let subExpression = expression.split('(').pop().split(')')[0];
                console.log(subExpression)
                parsedString.push(Calculator(subExpression))
                console.log(parsedString)
                expression = expression.substring(expression.indexOf(')')+1)
                console.log(expression)
                i=0;
                
            }
            else{
                parsedString.push(expression[i]);
            }    
        }
        
    }
    return parsedString;
}
function Calculator(str) {  
  // code goes here
  let parsedExpression = parseCalculationString(str);
  const operations = ['*', '/', '+', '-']
  for (i = 0; i < operations.length; i++){
    for(j = 0; j < parsedExpression.length; j++){
        if(operations[i] == parsedExpression[j]){
            if(operations[i] == '*'){
                Array.prototype.splice.apply(parsedExpression, [j-1, 3].concat(parsedExpression[j-1]*parsedExpression[j+1]));
            }
            else if(operations[i] == '/'){
                Array.prototype.splice.apply(parsedExpression, [j-1, 3].concat(parsedExpression[j-1]/parsedExpression[j+1]));
            }
            else if(operations[i] == '+'){
                Array.prototype.splice.apply(parsedExpression, [j-1, 3].concat(parsedExpression[j-1]+parsedExpression[j+1]));
            }
            else if(operations[i] == '-'){
                Array.prototype.splice.apply(parsedExpression, [j-1, 3].concat(parsedExpression[j-1]-parsedExpression[j+1]));
            }
        } 
        
      }
  }
  
  str = parsedExpression[0];
  
  return str; 

}

我的输出:

2+1 
(1) [3]
/(2+1)+4 
2+1 
+4

我的预期输出:

3*2
[6]
/(2+1)+4
2+1
[6,/,3]
+4
<script>
    console.log("===>", Calculator("(3*2)/(2+1)+4"));
    //Should get 3
    function parseCalculationString(expression) {
        let parsedString = [];
        for (i = 0; i < expression.length; i++) {
            if (expression[i] != " ") {
                if (expression[i] >= '0' && expression[i] <= '9') {
                    parsedString.push(parseFloat(expression[i]));
                }
                else if (expression[i] == '(') {
                    let subExpression = expression.split('(')[1].split(')')[0];
                    console.log(subExpression)
                    parsedString.push(Calculator(subExpression))
                    console.log(parsedString)
                    expression = expression.substring(expression.indexOf(')') + 1)
                    console.log(expression)
                    i = 0;
                }
                else {
                    parsedString.push(expression[i]);
                }
            }

        }
        return parsedString;
    }
    function Calculator(str) {
        // code goes here
        let parsedExpression = parseCalculationString(str);
        const operations = ['*', '/', '+', '-']
        for (i = 0; i < operations.length; i++) {
            for (j = 0; j < parsedExpression.length; j++) {
                if (operations[i] == parsedExpression[j]) {
                    if (operations[i] == '*') {
                        Array.prototype.splice.apply(parsedExpression, [j - 1, 3].concat(parsedExpression[j - 1] * parsedExpression[j + 1]));
                    }
                    else if (operations[i] == '/') {
                        Array.prototype.splice.apply(parsedExpression, [j - 1, 3].concat(parsedExpression[j - 1] / parsedExpression[j + 1]));
                    }
                    else if (operations[i] == '+') {
                        Array.prototype.splice.apply(parsedExpression, [j - 1, 3].concat(parsedExpression[j - 1] + parsedExpression[j + 1]));
                    }
                    else if (operations[i] == '-') {
                        Array.prototype.splice.apply(parsedExpression, [j - 1, 3].concat(parsedExpression[j - 1] - parsedExpression[j + 1]));
                    }
                }

            }
        }
        str = parsedExpression[0];
        return str;
    }
</script>

// 刚刚更新了以下行

let subExpression = expression.split('(').pop().split(')')[0];

let subExpression = expression.split('(')[1].split(')')[0];