Q:

Concatenate operator in math operation in Javascript

I want to create a random generator of math operations. I'm trying with ASCII codes but what happens is that it just concatenate the operands and operators as a String. Anyone has a suggestion for this?

let a = Math.floor(Math.random()*100)
let b =  Math.floor(Math.random()*100)
let ascCode = Math.floor(Math.random()* (46 - 42)) + 42
let op = String.fromCharCode(ascCode)
let c = a + `${op}` + b;
 
console.log(c)

A:

You can use eval:

The eval() function evaluates JavaScript code represented as a string.

let a = 1
let b = 2
let ascCode = Math.floor(Math.random()* (46 - 42)) + 42
let op = String.fromCharCode(ascCode)
let c = eval(a + `${op}` + b);

console.log(c)

But eval can be troublesome.

eval() is a dangerous function, which executes the code it's passed with the privileges of the caller. If you run eval() with a string that could be affected by a malicious party, you may end up running malicious code on the user's machine with the permissions of your webpage / extension. More importantly, a third-party code can see the scope in which eval() was invoked, which can lead to possible attacks in ways to which the similar Function is not susceptible.
eval() is also slower than the alternatives, since it has to invoke the JS interpreter, while many other constructs are optimized by modern JS engines.

msdn
Another solution would be to use some if:
let a = 1
let b = 2
let ascCode = Math.floor(Math.random()* (46 - 42)) + 42
let op = String.fromCharCode(ascCode)

let c = 0;
if (op == "*")
c = a + b;
if (op == "+")
c = a + b;
if (op == "-")
c = a - b;

/*etc*/ 

console.log(c)

or even a map where keys are operators and values are functions:
let a = 1
let b = 2
let ascCode = Math.floor(Math.random()* (46 - 42)) + 42
let op = String.fromCharCode(ascCode)

let operators = {
'+': (a, b) => a + b,
'-': (a, b) => a - b,
'*': (a, b) => a * b,
'/': (a, b) => a / b, // handle zero!
'%': (a, b) => a % b // handle zero!
};

console.log(operators[op](a,b))

A:

This solution is similar to things discussed in the comments.  It is a different, non-eval way of generating random math operations:

const ops = {
  '+': (a, b) => a + b,
  '-': (a, b) => a - b,
  '*': (a, b) => a * b,
  '/': (a, b) => a / b,
  '%': (a, b) => a % b,
  '>': (a, b) => a > b,
  '<': (a, b) => a < b,
  // etc
}

const randomOperation = ((ops) => {
  const keys = Object.keys(ops)
  const randomOpKey = () => keys[Math.floor(Math.random() * keys.length)]
  const evalOp = (key, a, b) => ops[key](a, b)
  
  return () => {
    const op = randomOpKey()
    const a = Math.floor(Math.random() * 100)
    const b = Math.floor(Math.random() * 100)
    return {
      expression: `${a} ${op} ${b}`,
      result: evalOp(op, a, b)
    }
  }
})(ops)


// demo

for (let i = 0; i < 20; i++) {
  let {expression, result} = randomOperation()
  console.log(`${expression} = ${result}`)
}

Note that what randomOperation returns is objects with two properties: expression as a string, and result as a value, which will be numeric or boolean.  The demo code shows one way to use it.  You have to manually maintain the list of operations, which is different than with the eval solutions.
I don't know what better suits your needs, but this should show that there are reasonable non-eval solutions possible.

