I am trying to do a specific algorithm with Javascript, and... it's a little bit tricky. Here is what i am trying to do : a console app (with nodejs and "readline"). I ask the user to make an operation, example : "2 + 3 * 4 / 2 - 1" what I want to do, is to read the string, turn it into an array, identify the priority operator (* and /), and then, replace (without regex) the operation at the place where previously there was the operation, and so on. With my example, it should do : [2 + 12 / 2 - 1] => [2 + 6 - 1] => [8 - 1] => [7]. So far, i succeeded in making an app that can add with many operators, but I got 2 problems : 1/ I dont know how to make the priority 2/ my app does not support more than 3 operations...
Here is my code, I appreciate any helps to undrstand what I am doing wrong and any help to finally end this algo. Thank you :
const readline = require("readline-sync");
const operators = ["+", "-", "*", "/"];
const openParenthesis = ['(', '[', '{'];
const closeParenthesis = [')', ']', '}'];
var numericOperator = 0;
var operatorsList = []
var operatorArray = [];
var NumberA = 0;
var NumberB = 0;
var Total = 0;
var soloOperator = "";
var removeValFromIndex = [];
var indexOperator = [];
var opFound = "";
function askOperator() {
    operator = readline.question(`make an operation: `)
    operatorArray = operator.split(' ');
    console.log(operatorArray, operatorArray.length)
}
askOperator();
splitArray(operatorArray);
function splitArray(sentenceArray) {
    for (let i = 0; i < sentenceArray.length; i++) {
        opFound = operators.find(el => el == sentenceArray[i]);
        if(opFound == "*") {
            const findMultiplyer = (element) => element == opFound;
            indexOperator = sentenceArray.findIndex(findMultiplyer);
            soloOperator = sentenceArray[indexOperator];
            NumberA = sentenceArray[indexOperator - 1];
            NumberB = sentenceArray[indexOperator + 1];
            removeValFromIndex.push((indexOperator - 1), indexOperator, (indexOperator + 1));
            for (var j = removeValFromIndex.length -1; j >= 0; j--){
                sentenceArray.splice(removeValFromIndex[j],1);
            }
        } else if (opFound == "/") {
            const findDivider = (element) => element == opFound;
            indexOperator = sentenceArray.findIndex(findDivider);
            soloOperator = sentenceArray[indexOperator];
            NumberA = sentenceArray[indexOperator - 1];
            NumberB = sentenceArray[indexOperator + 1];
            removeValFromIndex.push((indexOperator - 1), indexOperator, (indexOperator + 1));
            for (var j = removeValFromIndex.length -1; j >= 0; j--){
                sentenceArray.splice(removeValFromIndex[j],1);
            }
            
        } else if (opFound == "+") {
            const findAdd = (element) => element == opFound;
            indexOperator = sentenceArray.findIndex(findAdd);
            soloOperator = sentenceArray[indexOperator];
            NumberA = sentenceArray[indexOperator - 1];
            NumberB = sentenceArray[indexOperator + 1];
            removeValFromIndex.push((indexOperator - 1), indexOperator, (indexOperator + 1));
            for (var j = removeValFromIndex.length -1; j >= 0; j--){
                sentenceArray.splice(removeValFromIndex[j],1);
            }
        } else if (opFound == "-") {
            const findMinus = (element) => element == opFound;
            indexOperator = sentenceArray.findIndex(findMinus);
            soloOperator = sentenceArray[indexOperator];
            NumberA = sentenceArray[indexOperator - 1];
            NumberB = sentenceArray[indexOperator + 1];
            removeValFromIndex.push((indexOperator - 1), indexOperator, (indexOperator + 1));
            for (var j = removeValFromIndex.length -1; j >= 0; j--){
                sentenceArray.splice(removeValFromIndex[j],1);
            }
        }
        console.log("loop", opFound, "la", removeValFromIndex ,sentenceArray)
    }
    
console.log("test", indexOperator, "other", soloOperator, NumberA, NumberB);
doMath(NumberA, NumberB)
}
function doMath(numA, numB) {
    console.log("index in math", indexOperator)
        switch (soloOperator) {
            case '+' :
                Total = (parseInt(numA) + parseInt(numB));
                // operatorArray trouver * ou / si cest le cas on saute cette section
                if (indexOperator > 1) {
                    operatorArray.splice((indexOperator), 0, Total.toString())
                } else {
                    operatorArray.splice((indexOperator -1), 0, Total.toString())
                }
                if (operatorArray.length >= 3) {
                    return splitArray(operatorArray)
                }
                console.log("addition", Total, "new array", operatorArray );
                break;
            case '-' :
                Total = numA - numB;
                if (indexOperator > 1) {
                    operatorArray.splice((indexOperator), 0, Total.toString())
                } else {
                    operatorArray.splice((indexOperator -1), 0, Total.toString())
                }
                if (operatorArray.length >= 3) {
                    return splitArray(operatorArray)
                }
                console.log("substract", Total, "new array", operatorArray);
                break;
            case '*' :
                Total = numA * numB;
                if (indexOperator > 1) {
                    operatorArray.splice((indexOperator), 0, Total.toString())
                } else {
                    operatorArray.splice((indexOperator -1), 0, Total.toString())
                }
                if (operatorArray.length >= 3) {
                    return splitArray(operatorArray)
                }
                console.log(indexOperator,"multiply", Total, "new array", operatorArray);
 
                break;
            case '/' :
                Total = numA / numB;
                if (indexOperator > 1) {
                    operatorArray.splice((indexOperator), 0, Total.toString())
                } else {
                    operatorArray.splice((indexOperator -1), 0, Total.toString())
                }
                if (operatorArray.length >= 3) {
                    return splitArray(operatorArray)
                }
                operatorArray.splice((indexOperator), 0, Total.toString())
                console.log("divide", Total, "new array", operatorArray);
                break;
        
            default:
                console.log("An error occured")
                break;
        }
}