0

I have a piece of code (which I believe is very inefficient and bad; I may be wrong though.), that checks whether a variable is in range determined by some other variables, like so;

if ((a >= (x.eq(0) && y.eq(0)) || (a >= x.eq(1) && y.eq(1)) ... || (a >= x.eq(n) && y.eq(n)))) {
    // code here
}

(I might have gotten the amount of brackets wrong in the question, but the code itself works.)

where n is the total amount of numbers in arrays x and y.. Obviously, the if condition looks very, very large and I believe it's unoptimized and "bad". Is there anything I can use to make the condition shorter? In case the block above is unreadable, what I want in pseudocode:

Check if a is between x(0) and y(0).
If true, do things.
Else check if a is between x(1) and y(1).
If true, do things.
Else check ... if a is between x(n) and y(n).
If true, do things.
Else do nothing.

  • 2
    Once if statements become unreadable, it often pays to consider moving the functionality into a switch statement. Check out this question regarding the [use of ranges in switch statements](https://stackoverflow.com/questions/5619832/switch-on-ranges-of-integers-in-javascript). – Sandman Jun 14 '17 at 10:35
  • how big is `n`? is it always the same of to do, if in range, or different? – Nina Scholz Jun 14 '17 at 10:37
  • 1
    ... or assign logical groups to named variables so the final statement reads more like a sentence. `if(xyAreEqual && xInRangeZ) // etc.` – Emissary Jun 14 '17 at 10:37
  • please add what tag `array` has to do with the question. – Nina Scholz Jun 14 '17 at 10:49
  • @NinaScholz I was of the opinion that x and y are arrays, thus the tag? Fairly new to SO, so might be a mistake on my part. – Ivan Talanov Jun 14 '17 at 10:50
  • but if so, where is the index? – Nina Scholz Jun 14 '17 at 10:51
  • The index of...x and y, I'm assuming? That's the thing, I was using the indexes manually in the large if condition via the use of eq. Or, well, if it's not that I might not have understood you correctly. – Ivan Talanov Jun 14 '17 at 10:59
  • maybe you add just the data to the question, you like to compare with. – Nina Scholz Jun 14 '17 at 11:03

3 Answers3

1

You could use a for loop and exit if one condition is met. You need to use the right comparison for a range chekc with a and the left and right border.

var i;
for (i = 0; i <= n; i++) {
    if (x.eq(i) <= a && a <= y.eq(i)) {
        // do something
        break;
    }
}

Assuming you have two arrays with the corresponding length, or just one with an object with x and y property, you could use Array#some

array.some(function (o, i) {
    if (o.x <= a && a <= o.y) {
        // do something
        // update(o, i);
        return true;      // exit iteration
    }
});
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
1

You can create a reusable function which makes a range of numbers and checks if your condition is met:

// usage: 
//  isBetweenAny
//      (0,10) // start and end of range
//      (x, y) // functions, in your case in question pass x.eq and y.eq
//      (a) // your 'a' value
// returns true or false

const isBetweenAny = (startNum, endNum) => (x, y) => a => 
  [...Array(endNum + 1).keys()].slice(startNum) // range of numbers (inclusive)
    .some(num => (a >= x(num) && a <= y(num))) // if any number satisfies the condition

// identity function - for easy testing
const map = x => x;

const res1 = isBetweenAny(1, 10)(map, map)(1) // true: 1 in [0...10] range
const res2 = isBetweenAny(2, 10)(map, map)(1) // false: 1 not in [2...10] range

console.log(res1, res2)

Then you can also use it like this:

const mySpecificCase = isBetweenAny(0, 100)(x.eq, y.eq) // new function for your specific case

if (mySpecificCase(a)) {
 ....
}
Egor Stambakio
  • 17,836
  • 5
  • 33
  • 35
0

Do you want something like this?

// try every number between 0 and n
for (var i = 0; i++, i < n) {
  // generic version of your condition
  if (a >= (x.eq(i) && y.eq(i)) {
    // do the things
    break; // break the loop so the things are only done once
  }
}
Daniel Diekmeier
  • 3,401
  • 18
  • 33
  • Pretty much! Follow-up question though, would it be possible (and a good idea) to wrap the whole thing in a function and use the index parameter instead of declaring i, or is it overcomplicating a fairly simple task? – Ivan Talanov Jun 14 '17 at 10:49
  • @IvanTalanov I think that depends on your use case. A function with a good name can help with readability, but you have to decide if it's worth it. Good luck! :D – Daniel Diekmeier Jun 14 '17 at 10:52
  • As @Sandman stated in the comments, the most elegant solution (for me, at least, since the value of n wasn't exactly large (<10). However; I believe that for larger pools in "n", both your and Nina's solution are correct. Thank you! – Ivan Talanov Jun 14 '17 at 11:41