Take this array of math expressions, which are NOT strings:
let eqs = [ 1+n, 2+n, 3+n]
Is it possible to loop through these equations and modify them WITHOUT evaluating them in javascript somehow?
eqs.map(eq => eq + 1)
...and return this:
// [ 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...but I am getting the feeling that this might NOT be possible...is that the case?
Thank you for any assistance :-)
This could be a starting point:
let eqs = [ "1+n", "2+n", "3+n"]
let modeqs = eqs.map(eq => eq+"+1")
console.log(modeqs);
[0,8,15].map(n=>console.log(modeqs.map(s=>(new Function('n','return '+s))(n))))
The updated snippet now contains a part that converts each formula into a function and then executes it in a .map() structure.
Alternatively you could start with and 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.
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.
You can use Template literals (Template strings)
Demo :
let eqs = ['1+n', '2+n', '3+n'];
let res = eqs.map(eq => `${eq}+1`);
console.log(res);