1

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.

Bikram Kumar
  • 393
  • 1
  • 4
  • 15
  • 2
    `![b, s].includes(i)`? Though that uses the [SameValueZero algorithm](//tc39.es/ecma262/#sec-samevaluezero) which is closer to `===` and `!==`, which you should be using anyway. – Sebastian Simon May 29 '21 at 15:16
  • 1
    Duplicate of [Check variable equality against a list of values](/q/4728144/4642212) _plus_ [De Morgan’s laws](//en.wikipedia.org/wiki/De_Morgan%27s_laws): `i != b && i != s` is the same as `!(i == b || i == s)`. – Sebastian Simon May 29 '21 at 15:17
  • Did you mean `b.x2` instead of `b.x1`, when comparing to `s.x2`? – Bergi May 29 '21 at 16:10
  • @Bergi No, it is b.x2 (the x+width of bullet object). I am using it for collision detection. I want to make the condition to be written in shorter way by combining the two b.x1 for comparison with s.x1 and s.x2 as given in the question. – Bikram Kumar May 29 '21 at 16:15
  • @BikramKumar It's just weird to have such asymmetric code, it's the only one of four comparisons that compares a `…1` to a `…2`. Looks like a bug to everyone reading this. If it's on purpose, add a comment with the rationale – Bergi May 29 '21 at 16:47
  • @Bergi It's not only one. There are many situations where we would be comparing one variable with more that one variables. It is not always that we would compare only `x < y && a == b` and something that would include only unique variables with unique ones. See, we need large number of comparisons with only one or two variables in different ways, when the code gets complex. In fact, in collision-detection I came across situations when a single variable needs to be compared with two or more variables, like `a.x1 < b.x2 && a.x1 > b.x1 && a.x1 != N `. This is only just one example. – Bikram Kumar May 30 '21 at 03:52
  • @Bergi What I want to say is compressing these logical comparisons would highly facilitate in writing codes. For example, if I could get a syntax to write the above operation in short form, i.e. just write that `a.x1 is less than b.x2 and greater than b.x1 but not equal to N` without repeating `a.x1` again and again, by just using operators, it would be very simple to write this. I know it seems quite unusual in coding, but I was just asking about it if it is possible or not, and that is the community of stack overflow for is, isn't it? – Bikram Kumar May 30 '21 at 03:58
  • @BikramKumar There are no operators to abbreviate this. What you really want is a method: `a.collidesWith(b, N)`. – Bergi May 30 '21 at 09:56

2 Answers2

3

Not exactly, but you can do something like:

return ![b, s].includes(i);
Shravan Dhar
  • 1,455
  • 10
  • 18
  • There is no one way for all the cases. Although, you can create utility function (specific to your case). `function isStrictlyOutOfBounds(lower, upper, value) { return value < lower && value > upper; }` – Shravan Dhar May 30 '21 at 05:21
1

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.

Jogi Vab
  • 51
  • 4