0
how to assign a function to a variable?. For example y = f(x), where f is a polynomial
I need to assign a mathematical function to a variable
2 Réponses
+ 1
or shorter, if you use arrow function:
var f = x => x**2 + 5*x + 6;
... but arrow functions aren't supported by a few old mobile devices and some old browser versions; so for maximum compatibility you could use function expression (anonymous or not):
var f = function(x) { return Math.pow(x,2) + 5*x + 6; };
The use of Math.pow instead of the ** pow operator is required, because browsers/devices not supporting arrow function commonly don't also support it ;)
You could give a name to the function (function name(x)), wich could be useful when debugging (what you cannot do with artiw functions, wich are necessarly anonymous ^^)
0
I already found the solution
var f = (x) => { return x**2 + 5*x + 6; };