Home > Back-end >  Is it possible to alter Math Equations in a loop?
Is it possible to alter Math Equations in a loop?

Time:02-13

Take this array of math equations:

let eqs = [ 1 n, 2 n, 3 n]

Is it possible to loop through these equations and add to them in javascript somehow?

eqs.map(eq => eq   1)

...and return this:

let eqs = [ 1 n 1, 2 n 1, 3 n 1]

Is something like that even possible at all? Doesn't have to be .map() just using that as an example. Doesn't have to be that syntax, i just can't imagine what the syntax for something like that might be is all...

Been googling around but I haven't found much yet...

So thank you for any assistance :-)

CodePudding user response:

You can do that

let eqs = ['1 n', '2 n', '3 n']
console.log(eqs.map(eq => `${eq} 1`))

If you want another things . Comment below.

CodePudding user response:

This could be a starting point:

let eqs = [ "1 n", "2 n", "3 n"]
let modeqs = eqs.map(eq => eq " 1")
console.log(modeqs);

Or, if you want to manipulate actual functions:

let eqs = [ n=>1 n, n=>2 n, n=>3 n]
let modeqs = eqs.map(fn => eval(fn.toString() " 1") )
console.log(modeqs);

[0,8,15].map(n=>console.log(modeqs.map(fn=>fn(n))));

The last example uses eval() which by many is seen as a possible security risk. So, please think about where and when you want to use it.

  • Related