admin管理员组文章数量:1325708
Is there any maximum limit of conditions for If
in Javascript? I am using following method to detect bombs in division "b". But it skips more than 3 or 4 conditions and hence less bombcount than the actual count.
The following function triggers as soon as I click on div b
where b = some number
. It has a Kwhere I check for bomb in every cell and if the cell with the bomb satisfies the position criterion it increases bombcount by 1.
var i = 1;
var p1 = b-1;
var p2 = b+1;
var p3 = b+6;
var p4 = b-6;
var p5 = b-5;
var p6 = b+5;
var p7 = b-7;
var p8 = b+7;
var bombcount = 0;
while(i<37)
{
var check = document.getElementById(i).value;
if (check == "explode" && b>6 && b<31) {
if(i==p1 || i==p2 || i==p3 || i==p4 ||
i==p5 || i==p6 || i==p7 || i==p8) {
bombcount++
};
}
i++;
}
Is there any maximum limit of conditions for If
in Javascript? I am using following method to detect bombs in division "b". But it skips more than 3 or 4 conditions and hence less bombcount than the actual count.
The following function triggers as soon as I click on div b
where b = some number
. It has a Kwhere I check for bomb in every cell and if the cell with the bomb satisfies the position criterion it increases bombcount by 1.
var i = 1;
var p1 = b-1;
var p2 = b+1;
var p3 = b+6;
var p4 = b-6;
var p5 = b-5;
var p6 = b+5;
var p7 = b-7;
var p8 = b+7;
var bombcount = 0;
while(i<37)
{
var check = document.getElementById(i).value;
if (check == "explode" && b>6 && b<31) {
if(i==p1 || i==p2 || i==p3 || i==p4 ||
i==p5 || i==p6 || i==p7 || i==p8) {
bombcount++
};
}
i++;
}
Share
Improve this question
edited Apr 18, 2013 at 10:57
Abhishek Umrao
asked Apr 18, 2013 at 10:41
Abhishek UmraoAbhishek Umrao
1711 gold badge4 silver badges11 bronze badges
4
- No, there is no limit. If your code doesn't work as expect, you have to provide a more detailed explanation. – Bergi Commented Apr 18, 2013 at 10:46
-
Your code looks not very good. Try to use array for
p1..p8
and if will looks likeif (array.indexOf(i) != -1)
– Dmitry Volokh Commented Apr 18, 2013 at 10:47 - It might be a better idea to revisit your application's logic to avoid such conditionals. – adrianp Commented Apr 18, 2013 at 10:47
- there is no limit, but code looks not nice and hard to read. – Davor Mlinaric Commented Apr 18, 2013 at 10:47
3 Answers
Reset to default 4Use array for p
and indexOf to check is i
in array:
var p = [b - 1, b + 1, b + 6, b - 6, b + 5, b - 5, b + 7, b - 7];
if (p.indexOf(i) !== -1) {
bombcount++;
}
No, there is no limit to number of if-else
statements, be it one after another or nested if-else loops. Also, there is as such no limit to number of condition you can have under a if statement.
But, it is better to use switch
in those cases. It improves readability and performance.
For switch syntax see this https://developer.mozilla/en-US/docs/JavaScript/Reference/Statements/switch
For using switch over a range of values see this previous post Switch statement for greater-than/less-than
There is no limit. And as a tip, you maybe better off using switch
and falling through for these conditions:
switch (i) {
case p1:
case p2:
case p3:
// your logic
break;
}
https://developer.mozilla/en-US/docs/JavaScript/Reference/Statements/switch
本文标签: if statementMaximum conditions inside if in javascriptStack Overflow
版权声明:本文标题:if statement - Maximum conditions inside if in javascript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742195628a2431027.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论