By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,607 Members | 1,978 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,607 IT Pros & Developers. It's quick & easy.

How to Pass an Operator e.g., * / + -

P: n/a
function operator(a, b) {
return a + b;
}

without the use of eval, how would you change the + operator into say
* ? If you cannot pass operators in any form what would be the best
thing to do here or would the best thing to do in this case is use
eval?

Oct 27 '07 #1
Share this Question
Share on Google+
7 Replies


P: n/a
vbgunz wrote:
function operator(a, b) {
return a + b;
}

without the use of eval, how would you change the + operator into say
* ? If you cannot pass operators in any form what would be the best
thing to do here or would the best thing to do in this case is use
eval?
I don't believe this is possible then.

The list of operators is limited (*), so I would do something like
this:

function operator(a, b, op) {
if (op == 'plus') return a + b;
if (op == 'mult') return a * b;
if (op == 'minus') return a - b;
}

(*) http://www.w3schools.com/js/js_operators.asp

Hope this helps,

--
Bart

Oct 27 '07 #2

P: n/a

Bart Van der Donck wrote:
vbgunz wrote:
function operator(a, b) {
return a + b;
}

without the use of eval, how would you change the + operator into say
* ? If you cannot pass operators in any form what would be the best
thing to do here or would the best thing to do in this case is use
eval?

I don't believe this is possible then.

The list of operators is limited (*), so I would do something like
this:

function operator(a, b, op) {
if (op == 'plus') return a + b;
if (op == 'mult') return a * b;
if (op == 'minus') return a - b;
}

(*) http://www.w3schools.com/js/js_operators.asp

Hope this helps,

--
Bart

it all started with the following function. I am bad at math and dates
and while looking over some date methods I came up with this...

// Basic Date Arithmetic
var dateObject = new Date(2007, 9, 27);

// 01: this version can only do addition :/
function bda0(d, h, i) {
var d = new Date(d);
return new Date(d['set' + h](d['get' + h]() + i));
}
print(bda0(dateObject, 'Date', 5));

I was stumped and ended up doing something like what you mentioned
using the switch statement and a anonymous function. I didn't like it :
(

ultimately, this is what I have now but it uses eval. I really dislike
the idea of using eval but this was the shortest, most readable and
most concise version. please break it.

// 02: this version can add, multiply, divide and subtract
function bda(d, h, i) {
var d = new Date(d);
var o = /(^[+*/-])?(\w+)/g.exec(h)[1]; // operator
var h = /(^[+*/-])?(\w+)/g.exec(h)[2]; // handler
return eval("new Date(d['set' + h](d['get' + h]()" + o + "i));");
}
print(bda(dateObject, '+Date', 5));

I went looking through Number and Math and thought i might have missed
such operators but guess this may have to do. any suggestions?

Oct 27 '07 #3

P: n/a
vbgunz wrote:
function operator(a, b) { return a + b; }

[...] how would you change the + operator into say * ? [...] would the
best thing to do in this case is use eval?
eval() is designed exactly for those cases; see ES3 Final, 15.1.2.1.
PointedEars
--
realism: HTML 4.01 Strict
evangelism: XHTML 1.0 Strict
madness: XHTML 1.1 as application/xhtml+xml
-- Bjoern Hoehrmann
Oct 27 '07 #4

P: n/a
In comp.lang.javascript message <11**********************@z9g2000hsf.goo
glegroups.com>, Sat, 27 Oct 2007 06:52:11, vbgunz <vb****@gmail.com>
posted:
>--
Bart
See FAQ 2.3.
>it all started with the following function. I am bad at math and dates
and while looking over some date methods I came up with this...
Perhaps you did not see what our FAQ says about it?
>// Basic Date Arithmetic
var dateObject = new Date(2007, 9, 27);

// 01: this version can only do addition :/
function bda0(d, h, i) {
var d = new Date(d);
Inefficient (and unreliable for AD<100). Use
var d = new Date(+d);
return new Date(d['set' + h](d['get' + h]() + i));
= return d.setDate(d.getDate()+i) // etc.

Note that, while incrementing seconds and milliseconds is
straightforward (and can be done with getTime & setTime), doing it with
[minutes, ] hours, days, months, years can lead to surprises, since
[hours, ] days, months, years vary in length. Therefore, performing
simple addition without giving appropriate special treatment is unwise.
Consider adding a month to January 29th in Arizona.

It's a good idea to read the newsgroup c.l.j and its FAQ. See below.

--
(c) John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v6.05 IE 6
news:comp.lang.javascript FAQ <URL:http://www.jibbering.com/faq/index.html>.
<URL:http://www.merlyn.demon.co.uk/js-index.htmjscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/TP/BP/Delphi/jscr/&c, FAQ items, links.
Oct 27 '07 #5

P: n/a

Thomas 'PointedEars' Lahn wrote:
vbgunz wrote:
function operator(a, b) { return a + b; }

[...] how would you change the + operator into say * ? [...] would the
best thing to do in this case is use eval?

eval() is designed exactly for those cases; see ES3 Final, 15.1.2.1.
PointedEars
--
realism: HTML 4.01 Strict
evangelism: XHTML 1.0 Strict
madness: XHTML 1.1 as application/xhtml+xml
-- Bjoern Hoehrmann
Thank you, i downloaded the specification and will keep it close by
for reference. I usually don't approach a specification as I find
hieroglyphics a bit easier to understand but lately I've found myself
thinking with portals and knowing the cake is a lie. thank you for the
added enlightenment :)

Oct 29 '07 #6

P: n/a

Dr J R Stockton wrote:
In comp.lang.javascript message <11**********************@z9g2000hsf.goo
glegroups.com>, Sat, 27 Oct 2007 06:52:11, vbgunz <vb****@gmail.com>
posted:
--
Bart

See FAQ 2.3.
it all started with the following function. I am bad at math and dates
and while looking over some date methods I came up with this...

Perhaps you did not see what our FAQ says about it?
// Basic Date Arithmetic
var dateObject = new Date(2007, 9, 27);

// 01: this version can only do addition :/
function bda0(d, h, i) {
var d = new Date(d);

Inefficient (and unreliable for AD<100). Use
var d = new Date(+d);
return new Date(d['set' + h](d['get' + h]() + i));
= return d.setDate(d.getDate()+i) // etc.

Note that, while incrementing seconds and milliseconds is
straightforward (and can be done with getTime & setTime), doing it with
[minutes, ] hours, days, months, years can lead to surprises, since
[hours, ] days, months, years vary in length. Therefore, performing
simple addition without giving appropriate special treatment is unwise.
Consider adding a month to January 29th in Arizona.

It's a good idea to read the newsgroup c.l.j and its FAQ. See below.

--
(c) John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v6.05 IE 6
news:comp.lang.javascript FAQ <URL:http://www.jibbering.com/faq/index.html>.
<URL:http://www.merlyn.demon.co.uk/js-index.htmjscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/TP/BP/Delphi/jscr/&c, FAQ items, links.
thanks. I tried as you suggested with a slightly updated version:

function bda(d, h, i) {
var d = new Date(+d);
var h = /(^[+*/-])?(\w+)/g.exec(h).slice(1); // [operator, handler]
return eval("new Date(d['set' + h[1]](d['get' + h[1]]()" + h[0] +
"i));");
}
var dateObject = new Date(2007, 0, 29);
print(bda(dateObject, '+Month', 1));
// -Thu Mar 01 2007 00:00:00 GMT-0500 (EST)

I didn't expect the result I got and this is now newly added ammo to
my arsenal of programming conundrums. Although the answer from the
function is not what I expected (and probably not what any average Joe
would have expected), isn't the answer returned from the function
correct?

var dateObject = new Date(2007, 0, 1);
print(bda(dateObject, '+Date', 31));

I am bad at math and dates. reading up on them usually make me sleepy :
( I am in no way saying or even trying to imply you may be wrong and I
cannot thank you enough for your time and insight *but* isn't it
technically correct?

I am not using the code in any kind of production. it's a personal
academic exercise at best. I am interested in what you think if you
have the time.

ps. I been meaning to read the faqs in its entirety and originally
when making the post, I did scan the faqs and mailing list but was
looking for answers on how to pass an operator, I thought I may have
missed it :)

Oct 29 '07 #7

P: n/a
In comp.lang.javascript message <11**********************@o80g2000hse.go
oglegroups.com>, Mon, 29 Oct 2007 06:52:47, vbgunz <vb****@gmail.com>
posted:
>
Dr J R Stockton wrote:
>See FAQ 2.3.
>ps. I been meaning to read the faqs in its entirety and originally
when making the post, I did scan the faqs and mailing list but was
looking for answers on how to pass an operator, I thought I may have
missed it :)
When you have invested the effort needed to read and understand the
newsgroup FAQ, with particular reference to the cited section and to the
other part about dates, then I might renew my interest in your self-
inflicted difficulties.

It's a good idea to read the newsgroup c.l.j and its FAQ. See below.

--
(c) John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v6.05 IE 6
news:comp.lang.javascript FAQ <URL:http://www.jibbering.com/faq/index.html>.
<URL:http://www.merlyn.demon.co.uk/js-index.htmjscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/TP/BP/Delphi/jscr/&c, FAQ items, links.
Oct 29 '07 #8

This discussion thread is closed

Replies have been disabled for this discussion.