Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

js get result of 2 or more numbers with mathematical expressions

I have a counting-Discord bot which works perfectly fine, but i want to add some specials. Users should can use mathematical expressions like +;-;/;*, but i can’t get the result of the string

example:
console.log(+"14+2") or console.log(Number.parseInt(+"1+3*5-1"))
all equals NaN or just the first number.

i just found on another stackoverflow question how i can if it’s a number like true and false, but not the result.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You could just use math.js:

console.log(math.evaluate("14+2"))
console.log(math.evaluate("1+3*5-1"))
<script src="https://cdnjs.cloudflare.com/ajax/libs/mathjs/11.0.1/math.js"></script>
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading