Using JavaScript to evaluate simple numeric expression INPUT BY the user.

K

Ken Hilton

Greetings,

I'm mocking up a spreadsheet-like feature for a large legacy application and
I'm using HTML + JavaScript to do it: HTML provides the simple grid and
input fields, etc. and JavaScript handles the "spreadsheet" logic. Since
this is a mockup for early usability tests, I'd really rather not spend time
writing an expression parser/evaluator in JS to allow formula to be input
into spreadsheet cells (which when parsed and evaluated result in the
quantity displayed in the spreadsheet cell.) And, using a 3rd party grid
control is not likely an option. However, some level of such functionality
would be good for our usability study.
My question hence is this: suppose one has an HTML text <INPUT> field
into which a user types a simple, syntactically correct JavaScript
expression, e.g., "result=1+(2*3)". Is there anyway in JavaScript to have
that expression text string evaluated and the value of the JS variable
"result" set accordingly (say "result" is a predefined global JS variable,
if need be)?
Having been off of JavaScript for a couple of years, I was at a loss for
a solution when suddenly I came up w/the "brilliant" idea of dynamically
generating a JavaScript function that wrapped the expression string input by
the user. Then, write this JS function definition string to the document
and simply call it directly to have the expression evaluated. This worked
wonderfully!!!! ... except for the fact that the containing page was
completely replaced by me calling document.write(...) to inject the JS
wrapper function after the page was initially loaded. LOL. Any help you
can lend would be sincerely appreciated.

Ken.
 
L

Lee

Ken Hilton said:
My question hence is this: suppose one has an HTML text said:
into which a user types a simple, syntactically correct JavaScript
expression, e.g., "result=1+(2*3)". Is there anyway in JavaScript to have
that expression text string evaluated and the value of the JS variable
"result" set accordingly (say "result" is a predefined global JS variable,
if need be)?

result=0;
eval("result=1+(2*3)");
alert(result);

Beware that syntactically incorrect JavaScript may fail
without any error message, and that it's possible for
particularly bad entries to clobber other variables and
functions.
 
I

Ivo

"Ken Hilton" typed
My question hence is this: suppose one has an HTML text <INPUT> field
into which a user types a simple, syntactically correct JavaScript
expression, e.g., "result=1+(2*3)". Is there anyway in JavaScript to have
that expression text string evaluated and the value of the JS variable
"result" set accordingly (say "result" is a predefined global JS variable,
if need be)?

You 're thinking way and way too complicated. Google for
javascript+calculator or even javascript+spreadsheet and you will find many
scripts which do this sort of thing. It is I believe one of the rare
occasions where the eval() method is not misused. It would help to see some
of your current code to put it's uses into practice.
Ivo
 
R

Randy Webb

Ivo said:
"Ken Hilton" typed



You 're thinking way and way too complicated. Google for
javascript+calculator or even javascript+spreadsheet and you will find many
scripts which do this sort of thing. It is I believe one of the rare
occasions where the eval() method is not misused. It would help to see some
of your current code to put it's uses into practice.

That would have to be one of the places where I would agree with you
about eval being "the best way", even though if the input was
constrained to simple math problems, it could be done without eval, but
I doubt it would be as efficient.
 
K

Ken Hilton

Thank you very much. I reviewed the docs for eval() but they didn't impress
possibility this upon me.

"Domo Arigato Gozai Mas",

Ken.
 
R

Richard Cornford

Randy said:
Ivo wrote:

That would have to be one of the places where I would agree with you
about eval being "the best way", even though if the input was
constrained to simple math problems, it could be done without eval,
but I doubt it would be as efficient.

For various reasons I was thinking about javascript spreadsheets
recently and I think they can be more efficiently done without the use
of - eval - (just convoluted function/closure stuff instead). But I
would agree that it isn't worth it for a mock-up.

However, - eval - isn't the only string to executing code option. In a
spreadsheet I would expect much of the entered code to need to be
repeatedly re-executedd without modification. So using the strings of
source code with the - Function - constructor might have advantages

Richard.
 
D

Dr John Stockton

JRS: In article <P7yqc.76714$xw3.4235287@attbi_s04>, seen in
My question hence is this: suppose one has an HTML text <INPUT> field
into which a user types a simple, syntactically correct JavaScript
expression, e.g., "result=1+(2*3)". Is there anyway in JavaScript to have
that expression text string evaluated and the value of the JS variable
"result" set accordingly (say "result" is a predefined global JS variable,
if need be)?


See
<URL:http://www.merlyn.demon.co.uk/js-quick.htm#Ev>
<URL:http://www.merlyn.demon.co.uk/js-demos.htm#Ev>
<URL:http://www.merlyn.demon.co.uk/js-other.htm#eval>
<URL:http://www.merlyn.demon.co.uk/js-index.htm#In> Example
<URL:http://www.merlyn.demon.co.uk/js-logic.htm#ET>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,278
Latest member
BuzzDefenderpro

Latest Threads

Top