0

I am not sure if 'variable' is the correct word to use here but essentially, I would like to create a variable operator using JavaScript.

So far, the best thing I can think of is to create a switch statement but it seems very repetitive and tedious to do. Is there any way I can write one "If Statement" with a conditional operator?

What I have thought of:

variableOperator(x, y, operator) {
    switch(operator) {
        case '>=':
            if (x >= y) {
                // logic
            }
            break;
        case '<=':
            if (x <= y) {
                // logic
            }
            break;
        case '<':
            if (x < y) {
                // logic
            }
            break;
        case '<':
            if (x < y) {
                // logic
            }
            break;
    }
}

What I am wondering if is possible in pseudo-code (code below will obviously not work but in concept, this is what ideally I would like to use). No matter what the operator is (greater than, equals to, etc.) this if statement can take care of it:

variableOperator(x, y, operator) {
    if (x operator y) {
        // logic
    }
}

Edit: I should have clarified: x and y don't always have to be number types.

Mainly, I'm wondering about string (a-z,A-Z,0-9) comparison (== or !=) and number comparison (<, >, <=, >=). The project I'm working on does not need to worry about emoji's or other complex characters.

In essence, regardless of whether I'm comparing strings or numbers, I'm wondering if the operator itself can be a variable that can be easily manipulated/changed so that one would't have to write a separate bit of code for each case.

9
  • No, these are language level instruments we don't have the ability to manipulate in that way. Commented Feb 18, 2021 at 19:51
  • Are these the only operators you are concerned with? And will a and b only be Number types? Commented Feb 18, 2021 at 19:52
  • What do you expect variableOperator("foo", "bar", "👍") to produce? Commented Feb 18, 2021 at 19:54
  • @RandyCasburn/VLAZ - Good catch. I hope I clarified in my edit above in the post. Commented Feb 18, 2021 at 20:01
  • My original comment stands. The answer is no. But your premise is still flawed as far as I can tell. In your example you have 4 conditional statements in 4 cases with independent logic based upon the conditional - yet your ideal solution would have you pass an operator somehow and only use a single bit of logic? It seems logical that you would still need 4 independent bits of logic - where would those go? Commented Feb 18, 2021 at 20:07

1 Answer 1

2

Encode your operators in a lookup table and then you can use that in order to determine what to do. This reduces your code to just an if statement to check if the values pass the operator check. You can also include a fallback - if the operator is not found, just assume it fails.

const operators = {
  ">" : (a, b) => a > b ,
  ">=": (a, b) => a >= b,
  "<" : (a, b) => a < b ,
  "<=": (a, b) => a <= b,
  "==": (a, b) => a == b,
}

function doStuff(x, y, op) {
  const check = operators[op] ?? (() => false);
  
  if (check(x, y)) {
    console.log("do something");
  } else {
    console.log("ignore");
  }
}

doStuff(4, 2, ">");
doStuff(2, 4, "<");
doStuff("apple", "banana", "<");
doStuff(4, 2, "==");
doStuff(4, 2, "👎");

Sign up to request clarification or add additional context in comments.

1 Comment

Brilliant. I was going to recommend currying in a similar fashion, but this works.

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.