Skip to content

Instantly share code, notes, and snippets.

@ilanl
Last active February 22, 2020 10:36
Show Gist options
  • Save ilanl/b60defcea5e4be3ffbae11bc31baab19 to your computer and use it in GitHub Desktop.
Save ilanl/b60defcea5e4be3ffbae11bc31baab19 to your computer and use it in GitHub Desktop.
Use Reduce for Math Expression
const exp = '10+2*5*3+6';
const result = exp.split('+').reduce((added, e) => added += (e.split('*').reduce((multiplied, k) => multiplied *= parseInt(k), 1) ), 0 );
console.log(result);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment