最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - Shortening Multiple IF Conditions for the Same Variable - Stack Overflow

matteradmin6PV0评论

(first stackoverflow post) This is a javaScript question, but I think the logic applies anywhere

I've wondered for a long time about using an if statement to pare multiple values against the same variable without needing to type out the variable every time; specifically, in regards to "greater than" and "less than".

The specific example I'm trying to solve is:

var middle = 5;
if(middle < 10 && middle > 0) {
    //some stuff
}

I'm looking for something like:

var middle = 5;
if(middle (< 10 && > 0)) {
    //the same stuff
}

I have checked is-there-a-way-to-shorten-the-condition-if-it-always-pares-to-the-same-thing and if-statements-matching-multiple-values and if-statement-multiple-conditions-same-statement. I could not derive a simple answer to my specific question from those.

A final note: I do not want to make a new "multiple if" function that accepts arguments and then pares them for the variable. I am looking for a "built-in" way to do this.

Thanks, -Charles

(first stackoverflow post) This is a javaScript question, but I think the logic applies anywhere

I've wondered for a long time about using an if statement to pare multiple values against the same variable without needing to type out the variable every time; specifically, in regards to "greater than" and "less than".

The specific example I'm trying to solve is:

var middle = 5;
if(middle < 10 && middle > 0) {
    //some stuff
}

I'm looking for something like:

var middle = 5;
if(middle (< 10 && > 0)) {
    //the same stuff
}

I have checked is-there-a-way-to-shorten-the-condition-if-it-always-pares-to-the-same-thing and if-statements-matching-multiple-values and if-statement-multiple-conditions-same-statement. I could not derive a simple answer to my specific question from those.

A final note: I do not want to make a new "multiple if" function that accepts arguments and then pares them for the variable. I am looking for a "built-in" way to do this.

Thanks, -Charles

Share Improve this question edited May 23, 2017 at 12:16 CommunityBot 11 silver badge asked Mar 27, 2014 at 21:08 goodguy5goodguy5 131 silver badge4 bronze badges 3
  • how many range binding parisons do you actually need? Usually only two are necessary. – zzzzBov Commented Mar 27, 2014 at 21:10
  • 4 There is no such way. You can write a wrapper function that returns a boolean value, but you'll still have to write the logic out in that. – Etheryte Commented Mar 27, 2014 at 21:11
  • Huh... After some (failed) attempts, I've determined that it's not as easy as I thought to make the wrapper function without using eval(). Any tips on this side question? – goodguy5 Commented Mar 28, 2014 at 15:47
Add a ment  | 

5 Answers 5

Reset to default 4

Some languages like Python and Coffeescript (which piles to JS!) have chained parisons:

if (0 < middle < 10)

However, Javascript does not have these. Just as you already do, you will need to reference the variable twice:

if (0 < middle && middle < 10)

I don't believe it's possible, for a logical operator you need both sides of the operator to evaluate to true or false.

If you are missing a side the statement can't be evaluated.

Right, don't do this, but just for the sake of it :

if (Math.min(10, Math.max(0, middle)) == middle) {
   // middle is between 0 and 10
}

I am looking for a "built-in" way to do this.

Javascript doesn' t support this feature. In fact, most languages don't and Python is the only famous example I can think of right now that does that (you can write 0 < middle < 10 there).

In Javascript, the best you can do is use a shorter variable name

if(0 <= x && x < 10)

Or abstract mon tests into a function

if(between(0, x, 10))

If anyone is doing this in python for multiple values you can try this

exclude = ['10','0','20','30','12','14']
if i not in exclude:
   print(i)
Post a comment

comment list (0)

  1. No comments so far