admin管理员组文章数量:1026989
(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
5 Answers
Reset to default 4Some 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)
(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
5 Answers
Reset to default 4Some 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)
本文标签: javascriptShortening Multiple IF Conditions for the Same VariableStack Overflow
版权声明:本文标题:javascript - Shortening Multiple IF Conditions for the Same Variable - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745668154a2162296.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论