473,394 Members | 1,951 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

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

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
7 7266
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

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

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

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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: lokb | last post by:
Hi, I have a structure which and defined a smart pointer to the structure. /* Structure of Begin Document Index Record */ typedef struct BDI_Struct{ unsigned char rname; unsigned short int...
9
by: ceo | last post by:
Hi there, I'm reffering to a text that says following: "To summarize: When a copy of an object is generated because it passed to a function, the object's constructor function is not called....
110
by: Mr A | last post by:
Hi! I've been thinking about passing parameteras using references instead of pointers in order to emphasize that the parameter must be an object. Exemple: void func(Objec& object); //object...
9
by: Alvin Bruney | last post by:
The more knowledgable I get about this .net world, the more questions I have. ..NET uses pass by reference for all objects....uhhh I mean pass by value. (Couldn't resist this jab) Consider a...
10
by: Sean Dockery | last post by:
I have the following HTML file that I've been using for testing... <html> <head> <script type="text/javascript"> <!-- function handleWindowLoad() { var items = ; for (var i = 0; i < 11; i++)...
23
by: Sanjay Kumar | last post by:
Folks, I am getting back into C++ after a long time and I have this simple question: How do pyou ass a STL container like say a vector or a map (to and from a function) ? function: ...
8
by: Leo jay | last post by:
dear all, i want to write log on disk file when macro NEED_DO_LOG is defined. otherwise, do nothing. so i wrote code like this: #ifdef NEED_DO_LOG fstream fsLogFile (...); #else CDummyStream...
14
by: xdevel | last post by:
Hi, I need your help because I don't understand very well this: in C arguments are passed by-value. The function parameters get a copy of the argument values. But if I pass a pointer what really...
13
by: Francois Appert | last post by:
This post was originally in the C# Corner site, but their server is down. I'd like to see if this group can answer. I program in C++ and am learning C#. The issue is: why should anybody...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.