Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
298 views
in Technique[技术] by (71.8m points)

javascript - Why is for loop skipping part of string?

I am creating a calculator app that follows the order of operations. My issue is coming the else if on line 10 that checks for parentheses. What I want it to do it check for an open parentheses, run whats inside through my Calculator method recursively, push it into my parsed string array to be calculated again, remove the parentheses from my string, and then start i again at 0 to move the for loop to front of my string. For some reason it is skipping to the second parentheses and running it twice. Here is my code:

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; 

}

And my output:

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

And my expected output:

3*2
[6]
/(2+1)+4
2+1
[6,/,3]
+4

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
<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>

// just updated the following line

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

to

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...