简体   繁体   中英

Javascript — String Sums to Literal

How could I make this possible within Javascript? I have no idea where to start.

I want to be able to get a string such as var sum = "1+1-2.1*3/5"; and allow spaces too (we can strip spaces) — and then calculate it.

I've only ever done sums like this: var answer = 5+5; .

Any help would be appreciated. Thanks.

您可以简单地对变量使用eval进行数学运算。

eval(sum);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM