how to assign a function to a variable?. For example y = f(x), where f is a polynomial | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
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

3rd Apr 2020, 9:37 PM
Rafael Snider Bastidas
Rafael Snider Bastidas - avatar
2 Answers
+ 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 ^^)
3rd Apr 2020, 10:58 PM
visph
visph - avatar
0
I already found the solution var f = (x) => { return x**2 + 5*x + 6; };
3rd Apr 2020, 10:36 PM
Rafael Snider Bastidas
Rafael Snider Bastidas - avatar