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

V

vbgunz

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?
 
B

Bart Van der Donck

vbgunz said:
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,
 
V

vbgunz

Bart said:
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,


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?
 
T

Thomas 'PointedEars' Lahn

vbgunz said:
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
 
D

Dr J R Stockton

In comp.lang.javascript message <[email protected]

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.
 
V

vbgunz

Thomas said:
vbgunz said:
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 :)
 
V

vbgunz

Dr said:
In comp.lang.javascript message <[email protected]

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. [email protected] Turnpike v6.05 IE 6
FAQ <URL:http://www.jibbering.com/faq/index.html>.
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr 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 :)
 
D

Dr J R Stockton

In comp.lang.javascript message <[email protected]
Dr J R Stockton wrote:
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.
 

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
474,149
Messages
2,570,841
Members
47,388
Latest member
EarthaGilm

Latest Threads

Top