Search code examples
functionargumentsexpressionevaluate

Function with 3 arguments: Return result after applying operator i.e. (5, 7, '+') = 35


Hopefully my title explains it but I need a function that will take 3 arguments (two numbers and an operator [*, +, /, -]), then compute the result.

Something like this:

function evaluateExpression (firstNum, secondNum, operator) {
    ...
    return ...;
}

evaluateExpression (35, 7, '/'); // should return 5

Solution

  • Pseudocode:

    evaluateExpression( first, second, op ) :
    
        if op == "+" :
             return first + second
    
        else if op == "*" :
             return first * second
    
        ... etc
    
        else :
             return error