Skip to content

Instantly share code, notes, and snippets.

@techiepriyansh
Created March 17, 2018 10:43
Show Gist options
  • Save techiepriyansh/92e04b8bfab9c3b294ffa4faca1cd6af to your computer and use it in GitHub Desktop.
Save techiepriyansh/92e04b8bfab9c3b294ffa4faca1cd6af to your computer and use it in GitHub Desktop.
The simplest way to parse string algebra in javascript with 3 lines of code. This magical thing uses javascript's inbuilt ability to parse string algebra...
var makeFunctionOfX = function(src) {
return Function('x', 'return ' + src);
};
var g = makeFunctionOfX('2 * x')
var y = g(3); // y contains 6
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment