0

I have event listeners that push numbers and strings of math operators to an array so it looks like this:

var outputArr = [9 , "+" , 9];

I want to end up with this code:

var readyForMath = 9 + 9;

I've tried using .join("") to make it a string and then passing it into and eval function as so:

var evaluate = eval(outputArr.join(""));

which works but I don't want to use it because its bad practice.

I've tried parse functions such as parseInt() and parseFloat(). However, they return the number only and remove the strings completely. Does anyone know how to achieve this ?

0 Answers0