JS calculator, respecting order of operations I have a function that receives an array of numbers and operations

Ex: userEntry = [3,+,3,x,3] -&gt; returns 12


It is a calculator that adheres to the proper order of operations, therefore multiply/divide behaves differently than addition/subtraction. I have one redundant line of code:

userEntry = calculatorOperations
    .calculationSequence(operationsMD[i], indexOfOperand, userEntry);


is similar to:

 userEntry = calculatorOperations
    .calculationSequence(userEntry[1], indexOfOperand, userEntry);


With different behavior between operations, is it possible to remove this redundant line or is it neccesary?

function operateOnEntry(userEntry) {
//this is where the calculations occur when hitting =
const operationsMD = ['x', '/'];
let indexOfOperand;
let operation;

while (userEntry.includes('x') || userEntry.includes('/')) {
    let i = 0;
    if (!userEntry.includes('x')) {
        i++;
    }
    indexOfOperand = userEntry.indexOf(operationsMD[i]);
    userEntry = calculatorOperations
        .calculationSequence(operationsMD[i], indexOfOperand, userEntry);
}
while (userEntry.includes('+') || userEntry.includes('-')) {
    indexOfOperand = 1;
    userEntry = calculatorOperations
        .calculationSequence(userEntry[1], indexOfOperand, userEntry);
}
return userEntry;
}

let calculatorOperations = {
'x': (arg1, arg2) =&gt; {
    return arg1 * arg2;
},
'/': (arg1, arg2) =&gt; {
    return arg1 / arg2;
},
'+': (arg1, arg2) =&gt; {
    return arg1 + arg2;
},
'-': (arg1, arg2) =&gt; {
    return arg1 - arg2;
},
returnIndexOfEntry: (index, userEntry) =&gt; {
    let arg1 = Number(userEntry[index - 1]);
    let arg2 = Number(userEntry[index + 1]);
    return [arg1, arg2];
},
returnSpliced: (index, newTotal, userEntry) =&gt; {
    userEntry.splice((index - 1), 3, newTotal);
    return userEntry;
},
calculationSequence: (operation, indexOfOperand, userEntry) =&gt; {

    let getArgs = calculatorOperations.returnIndexOfEntry(indexOfOperand, userEntry);
    let newTotalForEntry = calculatorOperations[operation](getArgs[0], getArgs[1]);
    let newUserEntry = calculatorOperations.returnSpliced(indexOfOperand, newTotalForEntry, userEntry);
    return newUserEntry;
}
};
Overview


If you are using ES6, you should use the full ability of arrow functions: (args) =&gt; returnVal
In addition, some of the methods don't need to be defined as a property.
calculatorOperations should be a constant.
You don't need to access each element in an array like this: (getArgs[0], getArgs[1]), you can do (...getArgs)
I agree with @SamOnela in that you can iterate over the operations. However, their method pollutes global namespace


Questions


Why are you using x not *?


Rewrite



    function operateOnEntry(userEntry) {
        let indexOfOperand,
            operation;
        Object.keys(calculatorOperations.ops).forEach(function(functionName) {
            while (userEntry.includes(functionName)) {
                indexOfOperand = userEntry.indexOf(functionName);
                userEntry = calculatorOperations.calculationSequence(functionName, indexOfOperand, userEntry);
            };
        });
        return userEntry;
    }
    
    const calculatorOperations = {
        ops: {
            'x': (arg1, arg2) =&gt; arg1 * arg2,
            '/': (arg1, arg2) =&gt; arg1 / arg2,
            '+': (arg1, arg2) =&gt; arg1 + arg2,
            '-': (arg1, arg2) =&gt; arg1 - arg2
        },
        returnIndexOfEntry(index, userEntry) {
            let arg1 = Number(userEntry[index - 1]),
                arg2 = Number(userEntry[index + 1]);
            return [arg1, arg2];
        },
        returnSpliced(index, newTotal, userEntry) {
            userEntry.splice((index - 1), 3, newTotal);
            return userEntry;
        },
        calculationSequence(operation, indexOfOperand, userEntry) {
    
            let getArgs = calculatorOperations.returnIndexOfEntry(indexOfOperand, userEntry),
                newTotalForEntry = calculatorOperations.ops[operation](...getArgs),
                newUserEntry = calculatorOperations.returnSpliced(indexOfOperand, newTotalForEntry, userEntry);
            return newUserEntry;
        }
    };
    var userEntry = [3, '+', 3, 'x', 3];
    console.log(operateOnEntry(userEntry));