I have an array with the following value:
2, *, 5
How can I execute this string like so:
2 * 5
so the result returned is 10?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
The most horribly insecure way possible would be to do something like this:
eval([2,"*",5].join(''))But I could never recommend doing that, like ever. The “right” way to do it would be to write some kind of parser.
This would essentially work like a very simple calculator… but I still couldn’t really recommend this approach.
What are you trying to do exactly? Maybe there is a better way to model what you are trying to do other than an array?