Sagewire Logo

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

4 Message(s) by 3 Author(s) originally posted in javascript programming


From: vbgunz Date:   Saturday, October 27, 2007
function operator (a, b) {
return a + b;
}

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


From: Bart Van der Donck Date:   Saturday, October 27, 2007
wrote in message:

function operator(a, b) {
return a + b;
}
without the use of eval, how'd you change the + operator into say
* ? If you cannot pass operators in any form what'd be the best
thing to do here or'd the best thing to do in this case is use
eval?



I do not believe this is possible then.

The list of operators is limited (*), so I'd 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


From: vbgunz Date:   Saturday, October 27, 2007
wrote in message:
wrote in message:
> function operator(a, b) {
> return a + b;
> }
>
> without the use of eval, how'd you change the + operator into say
> * ? If you cannot pass operators in any form what'd be the best
> thing to do here or'd the best thing to do in this case is use
> eval?
I do not believe this is possible then.
The list of operators is limited (*), so I'd 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,
--
Bartit all started with the following function. I am bad at math and date s


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 did not 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?


From: Thomas PointedEars Lahn Date:   Saturday, October 27, 2007
wrote in message:
function operator(a, b) { return a + b; }
[...] how'd you change the + operator into say * ? [...]'d 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



Next Message: regex match function does not return any output


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

Java's Reflection API
Each returns an array of descriptor objects, eg Constructor, Field, Method. These classes in java.lang.reflect (and their parent class Member) provide detailed information about one member in the given class. ...

all new zealand I have a fairly quick newbie question to present ...
is there a way to put a command on the arg list of the interpreter to get it to run them then exit? eg python for x in range(14) : print x. Dude-X: I’ll check it out, thanks. python -c … (you can use different args to separate the lines ...

Java Interview Questions, FAQs
How does bitwise (~) operator work It converts all the 1 bits in a binary value to 0s and all the 0 bits to 1s, eg 11110000 coverts to 00001111 What is a modulo operator % This operator gives the value which is related to the remainder ...

Confluence Extension Template
SQL-4, pass parameters from one sql macro to another, Derek Stevenson, Open. Bug, SQL-3, Rich text editor can't handle SQL macro and drops it from page, Sam Peascod, Open. Bug, SQL-2, SQL Macro adds extraneous javascript to PDF export ...

Lexing and Parsing in Java
When I first added support for lists, the test needed some work, got it to pass, then I added another expression with nested lists, and that one also passed, but I wanted another test that would count the types of expressions in the AST ...

Re: Weird operator precedence with default operator AND
On Tue, October 09, 2007, Daniel Naber wrote: > The operator precedence is known to be buggy. You need to use parenthesis, > eg (aa AND bb) OR (cc AND dd) This would be fine with me but unfortunately not for my users. ...


Programming | Sports | Autos

copyright 2006
Valid XHTML 1.0 Transitional