"eval" question

About advanced scenes, and the Thyme scripting language used in Algodoo.

"eval" question

Postby Xray » Mon Jul 30, 2018 5:00 am

I may not be using it correctly, but here is what's happening....

eval("2 + 3") returns 5

Okay so far....

eval("2" + "3") returns 23

Seems strange, but I can live with that.

Now, here's the weird one:

eval("2 + 3" + "2 + 3") returns 37

Can anyone please explain WHY eval returns such an unexpected value? Is it a bug, or am I simply not understanding how it works?

Thanks
User avatar
Xray
 
Posts: 500
Joined: Sun Jun 17, 2012 6:12 am
Location: USA

Re: "eval" question

Postby FRA32 » Sun Aug 05, 2018 1:34 pm

the reason for this is because the operators inside the eval function are not actually part of the function itself, as the eval function only operates on strings. As such, eval("2+3"+"2+3") actually calculates eval("2+32+3") as the strings are concatenated, and 2+32+3 is 37
FRA32
 
Posts: 229
Joined: Wed Dec 03, 2014 9:51 pm

Re: "eval" question

Postby Xray » Sun Aug 05, 2018 6:33 pm

Thanks, FRA32! That explains the odd results I've been getting! :clap:
User avatar
Xray
 
Posts: 500
Joined: Sun Jun 17, 2012 6:12 am
Location: USA


Return to Thyme scripting

Who is online

Users browsing this forum: No registered users and 4 guests

cron