your issue can be resolved using eval, but as Hassan has already mentioned, eval can be very trick.
What you can do is create a function that parses the strings and does the operation:
function arithmetic(str) {
// Probably want to remove all spaces here, but I won't do that now lol
// Get the numbers based on whether there's a `+-/*` in the string
const numList = str.split(/[+-/*]/i);
const num1 = parseFloat(numList[0]);
const num2 = parseFloat(numList[1]);
// Create the regex to find the arithmetic operator
const regex = new RegExp(/[+-/*]/i);
// Run the correct operation on the numbers
switch(regex.exec(str)[0]) {
case '+':
console.log(num1 + num2);
break;
case '-':
console.log(num1 - num2);
break;
case '/':
console.log(num1 / num2);
break;
case '*':
console.log(num1 * num2);
break;
default:
console.error('Operation not found');
break;
}
}
You can even extend this code to do more complicated math, but hey that's up to you and whether you want to.