D
D
Danil Sitdikov2018-02-02 10:17:04
JavaScript
Danil Sitdikov, 2018-02-02 10:17:04

I have an array of elements. How to perform mathematical operations?

There is an array of type elems = ["12","+","4", "*", "2", "/", "34"].
How to bring it to the form res = 12 + 4 * 2 / 34 ?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Aligatro, 2018-02-02
@Quadrad_S

Same thing, but without eval.

function numToFn(fn) {
  return new Function('return ' + fn)();
}
elems = ["12","+","4", "*", "2", "/", "34"];

console.log( elems.join('') + ' = ' +  numToFn(elems.join('')) );

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question