Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

if/else defines plus or minus in function

I have two practically identical functions. The only difference between them are one has + and one has -. These are used about 20 times in each function, so I'd love to make this simpler with a var.

I'm still very new to JavaScript, so I don't know where to start, but something like:

if ( blah blah ){
    var symbol = '-';
} else{
    var symbol = '+';
}

var example = value1 [symbol] value2;

Thanks

like image 876
Jake Hills Avatar asked Oct 31 '13 13:10

Jake Hills


People also ask

How do you use if clause in C++?

Else You can use these conditions to perform different actions for different decisions. Use the if statement to specify a block of C++ code to be executed if a condition is true. Note that if is in lowercase letters.

What is else in C++ if else?

Else C++ If ... Else You can use these conditions to perform different actions for different decisions. Use the if statement to specify a block of C++ code to be executed if a condition is true. Note that if is in lowercase letters.

What is the difference between if and and and or?

AND – =IF (AND (Something is True, Something else is True), Value if True, Value if False) OR – =IF (OR (Something is True, Something else is True), Value if True, Value if False) Following are examples of some common nested IF (AND ()), IF (OR ()) and IF (NOT ()) statements.

How do you use the not function?

The NOT function only takes one condition. Here are the formulas spelled out according to their logic: IF A2 (25) is greater than 0, AND B2 (75) is less than 100, then return TRUE, otherwise return FALSE. In this case both conditions are true, so TRUE is returned.


1 Answers

Use this:

var modifier = (blah blah) ? 1 : -1;
var example = value1 + (modifier * value2);
like image 198
darthmaim Avatar answered Nov 15 '22 21:11

darthmaim