Search code examples
javascriptcollision-detectionlogical-operatorsinequality

Merging two comparisons with a common variable together


I have a function as follows:

bullets.forEach(function (b) {
        ships.forEach (function (s) {
          if (b.x1 >= s.x1 && b.x1 <= s.x2 && b.y1 >= s.y1 && b.y2 <= s.y2) {
             animationBucket = animationBucket.filter((i) => {
             return (i != b && i != s);
           }
           ); 

I want to merge those comparisons which use the same variables like i != b && i != s. I want it to be like i is not equal to b and s using i a single time. Is it possible? If yes, then how?

Edit: I will not be using the same inequality operator all the time. I want a code that should work for all comparisons like, i < j && i > k, i == b && i != s and so on. To be simple, I just want to make the variable (here i ) used two times (or more) to be used only once or maybe in shorter way.


Solution

  • There are not such things in JavaScript yet. But, maybe this may be added in future :) Till that you can use some helper functions to do your tasks.