0

What's the most efficient way to return true in a if() condition to check if a number is in between 2 other. My biggest problem is that the higher number and the lower number has to be exchangable(see examples). It also has to exclude the comparator (as in within more than in between?).

Examples

x=14, y=7 and we check if b=8 is between x and y: return true

x=6, y=12 and we check if b=11 is between x and y: return true

x=6, y=12 and we check if b=12 is between x and y: return false

x=12, y=6 and we check if b=12 is between x and y: return false

x=6, y=12 and we check if b=6 is between x and y: return false

11
  • 3
    @Kinduser - There is no reason for the ? true : false bit. Just (b < x && b > y) has the same effect. Commented Mar 1, 2017 at 20:40
  • 1
    x > y ? (b < x && b > y) : (b > x && b < y) Commented Mar 1, 2017 at 20:41
  • 1
    @SamuelCharpentier i believe so? why not try? :) or you can try JLRishe s method and call if(inbetween(x, y, b)) or you can extract what i suggested you into a method Commented Mar 1, 2017 at 20:44
  • 1
    Works perfectly! jsfiddle.net/scharpentier0/sj0kegva If you don't mind making an answer, I'll make it the selected answer. Commented Mar 1, 2017 at 20:52
  • 1
    @let_the_coding_begin if you didn't see, OP would like your comment as an answer — function between(x, y, b) { return x > y ? (b < x && b > y) : etc. (personally, I would use n in place of b) Commented Mar 1, 2017 at 21:19

4 Answers 4

2

You can use this:

function isBetween(x, y, b) {
    return b > Math.min(x, y) && b < Math.max(x, y);
}

console.log(isBetween(14, 7, 8));
console.log(isBetween(6, 12, 11));
console.log(isBetween(6, 12, 12));
console.log(isBetween(12, 6, 12));
console.log(isBetween(6, 12, 6));

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

7 Comments

I would argue this is "wrong" given the requirements. Yes it works, but he says, "What's the most efficient way" I cannot imagine that having to call Math.min and Math.max is more efficient than just a simple if statement.
@dman2306 Fair point, though it's efficient in terms of code length and readability. If OP wants the most efficient way to do something (in terms of execution time), then that's a very subjective question (and smells of micro-optimizations).
@dman2306 How can you declare anything the most efficient way to do anything that's not completely trivial? (Especially with the differences between JS engines).
@JLRishe a CPU has a finite set of instructions. There must exist some set of instructions that produces the optimal results given a particular task. Sure, there may be more than one optimal set, but there must be at least 1 optimal set. I can guarantee you this, creating stack frames to call methods is NOT the optimal way. There is a reason why in languages such as C/C++ min/max functions are often optimized as inline if statements during compilation.
@dman2306 Any decent JavaScript engine will inline Math.min and Math.max for you. I would not concern myself over such petty micro-optimizations and I certainly would not trash this [rather well-thought-out] answer over such a trivial matter.
|
2

the order does not matter if you evaluate them both

   function between(x, value1, value2) {
  return (x > value1 && x < value2) || (x < value1 && x > value2);
  }
  // ...
 if (between(x, 14, 7)) {
  // something
  }

EDIT: remember to use the (=> <=) OR (< >) accordingly to your needs either if you want to make a include between or an exclude between.

example:

   function between(x, value1, value2) {
  return (x >= value1 && x <= value2) || (x <= value1 && x >= value2);
  }

will return TRUE for between(7, 14, 7) or between(14, 14, 7)

while

   function between(x, value1, value2) {
  return (x > value1 && x < value2) || (x < value1 && x > value2);
  }

will return FALSE for between(7, 14, 7) or between(14, 14, 7)

2 Comments

OP says "It also has to exclude the comparator" so your <= and >= should be < and > to exclude the limits.
The question does state (as I quoted) "It also has to exclude the comparator" and an example x=12, y=6 and we check if b=12 is between x and y: return false demonstrates it. Your between(12, 12, 6) gets that answer wrong.
0

You can use

x > y ? (b < x && b > y) : (b > x && b < y)

to get the result you want. To use it in an if statement you can either use the equation directly or extract it into a method and substitute it into the if statement.

Here is also the link you provided so other people can see it all together :)

https://jsfiddle.net/scharpentier0/sj0kegva/

1 Comment

Anything of the form if (condition) { console.log("true"); } else { console.log("false"); } can be reduced to console.log( condition ); More generally, anything in the form if (condition) return true else return false can be reduced to return condition which is how I formulated it in my comment.
0

You can also take the functional programming approach:

const compileIsBetween = function (x, y) {
  const min = Math.min(x, y);
  const max = Math.max(x, y);
  return function (num) {
    return num > min && num < max;
  }
}

console.log(compileIsBetween(14, 7)(8));
console.log(compileIsBetween(6, 12)(11));
console.log(compileIsBetween(6, 12)(12));
console.log(compileIsBetween(12, 6)(12));
console.log(compileIsBetween(6, 12)(6));

const isBetween10and20 = compileIsBetween(10, 20);
console.log(isBetween10and20(12))
console.log(isBetween10and20(23))
console.log(isBetween10and20(2))

Comments

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.