Last active
February 22, 2020 10:36
-
-
Save ilanl/b60defcea5e4be3ffbae11bc31baab19 to your computer and use it in GitHub Desktop.
Use Reduce for Math Expression
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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