2

I am trying to replace some mathematical operators with variables, so that I can change the operator according to some conditions and then an then use that variable in place of the operator.

For example

var abc = 5;
if (abc<5)
// perform subtraction;
else if(abc==5)
// perform addition;
else if(abc>5)
// perform multiplication

Now what I want is that instead performing the operations based on the value of variable abc assign the operator it self to some variable and then use the variable in place of the operator, so that I can reduce some code and only one block of code can be used in place of 3 or 5 blocks. Something like this

var sign;
var result;
if(some condition)
sign = "-";
result = 5 sign 4;

I want to do this mainly in javascript.

4
  • 1
    This is not possible. Operators are not objects, you cannot "pass them around". Please explain what you actually want to do, best with a sample of your real code. Commented Apr 3, 2012 at 7:36
  • 1
    Conditions #1 and #3 are the same... Commented Apr 3, 2012 at 7:37
  • reference for different approach: en.wikipedia.org/wiki/Polish_notation Commented Apr 3, 2012 at 7:45
  • Hey man, have you considered changing the accepted answer on this question ? Commented Jul 30, 2014 at 17:08

5 Answers 5

11

refactor the several operations with operators you want to do in to different functions, then you can pass the different functions around, depending on what you want to do ( strategy pattern )

working example:

function multiply(a, b) {
    return a * b;
}
function substract(a, b) {
    return a - b;
}
function add(a, b) {
    return a + b;
}

var sign;
var result;
var abc = 10;
if( abc < 5 ) {
    sign = substract;
}else if( abc == 5 ) {
    sign = add;
}else if( abc > 5  ) {
    sign = multiply;
}

result = sign(5, 4);
Sign up to request clarification or add additional context in comments.

2 Comments

+1 for elegant way but still - might be overkill for the simple needs of the OP here.
Thank you for your up vote, i appreciate that. But i don't think it is overkill. It makes for more readable, more performance and more maintainable code, and it's not that much work to type out really.
1

You can use eval for this specific case:

var  op = "";
if (abc>5)
    op = "-";
else if(abc==5)
    op = "+";
else
    op = "*";
if (op.length > 0)
    alert(eval("10" + op + "10"));

This will perform "dynanic operation" on two numbers (10 and 10 in this example) based on the value of abc by storing the operator as a string then evaluate it.

Live test case.

8 Comments

Cheers - just be careful with eval and NEVER use it when user input is part of what you evaluate unless you double and triple check it. eval is way too powerful to be considered lightly.
using eval is definitely NOT the way to go for this approach, refactor your logic, seriously.
@helmus with all due respect, this solution works and provide simple way to achieve what the OP want. If you don't like it then don't use it but unless you can come with solid proof of extreme problem in the code, I won't change it.
eval should be avoided unless there's REALLY no other option, for two reasons: 1/it's expensive to call 2/it's dangerous. If for instance the operands are coming from user input, this can have SEVERE consequences. In this case there are better alternatives, so IMO helmus is right.
@Mohsen it just means people agree with many posts. I don't post to get points, I answer to help whoever have a problem.
|
1

JavaScript doesn't allow for passing operators as values.

What you might do instead is assign a function that uses the particular operator you want:

function add(a, b) { return a + b; }
function subt(a, b) { return a - b; }
function mult(a, b) { return a * b; }

var abc = 5, oper;

if (abc > 5)
    oper = subt;
else if (abc == 5)
    oper = add;
else
    oper = mult;

alert(oper(4, 3));

Comments

1

You cannot pass operators around as first class objects. You could push strings around but it's going to be ugly. Unfortunately this is not Lisp. However, operators are just functions that look sort of pretty to read. Just use functions. You can put conditional statements in those functions or simply override them

var abc = 5;
var compare = function (x,y) {
  return x > y;  
};

if (compare(abc,5)) {
// perform subtraction
}

You can just assign functions to variables and use those. You can override those functions later if you wish.

// We can override compare with some other function
compare = function (x,y) {
    return x < y;
}

Comments

0

In JavaScript, functions are first class objects. You can create them and pass them around like you would any other value. Say, perhaps, you want an "operator" that always adds 5 to its operand:

var addFive = function(operand){ return operand + 5; };
addFive(1) == 6;
addFive(5) == 10;

This function can also be passed around as an argument to other functions:

var higherOrder = function(number, operator){ return operator(number); };
higherOrder(1, addFive) == 6;
higherOrder(5, addFive) == 10;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.