0

I'm building a function which gets a variable that includes a mathematic operator, and two random numbers. I want to return the result of the suitable math action, but I'm not sure how the code should be written.

function result(firstnum, secnum, operator) {
return [???];
}
jonrsharpe
  • 107,083
  • 22
  • 201
  • 376
  • 2
    Create an object that maps operator names to functions, then call the function. – Barmar May 22 '18 at 20:58
  • 2
    StackOverflow is not a free coding service. SO expects you to [try to solve your own problem first](http://meta.stackoverflow.com/questions/261592). Please update your question to show what you have already tried in a [mcve]. For further information, please see [ask], and take the [tour] :) – Barmar May 22 '18 at 20:58
  • 1
    `return operators[operator](firstnum, secnum);` – Barmar May 22 '18 at 20:59
  • 1
    take a look at this solution https://stackoverflow.com/a/22303786/8414468 – TalGabay May 22 '18 at 21:00

0 Answers0