2

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++;
    }
Abhishek Umrao
  • 171
  • 1
  • 4
  • 11

3 Answers3

4

Use 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++;
} 
Yuriy Rozhovetskiy
  • 22,270
  • 4
  • 37
  • 68
  • Nice, but I'd advise to iterate over that `p` array instead of `i` and checking each element. – Bergi Apr 18 '13 at 10:52
  • its the same thing in shorter version. It still deosn't gives me right answer. skips few positions. :( – Abhishek Umrao Apr 18 '13 at 10:55
  • @AbhishekUmrao: Then likely those `b-x` values are computed wrong. But if you can't tell us what they're supposed to be, we hardly can fix them. – Bergi Apr 18 '13 at 11:00
  • @Bergi so `b-x` refer to the adjacent cell numbers. I am trying to learn java by build minesweeper. its a 6x6 table. – Abhishek Umrao Apr 18 '13 at 11:02
  • Yeah, I feared that. Try to represent the table as an array of arrays, so that you can use two indizes for x and y position. There's going something wrong with your current calculations - step through it with a debugger and you'll see how you are checking the wrong fields. – Bergi Apr 18 '13 at 11:27
  • Hey @Bergi thanks figured out what was going wrong `b+1` wasn't mathematically added but concatenate. Converting it to integer `b*1+1` solved the issue. Now it works like charm. – Abhishek Umrao Apr 18 '13 at 11:33
3

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.org/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

Community
  • 1
  • 1
AurA
  • 12,135
  • 7
  • 46
  • 63
  • 1
    @Bergi why not ? Do you feel that because of range based comparisons ... well you can do that too... coding a bit cleverly, see the updated answer – AurA Apr 18 '13 at 10:54
  • 1
    Because he has one and the same action to take when a value matches, instead of many different ones. `switch` is a replacement for lots of [`if`-`else`-statements](http://stackoverflow.com/q/10029089/1048572) (and maybe not even the best one), but absolutely not for a single condition. Did you intend to use one huge fall-through? – Bergi Apr 18 '13 at 10:59
  • @AurA how can you use `switch` here? it says if either of those conditions are satisfied then it will add to the bombcount. – Abhishek Umrao Apr 18 '13 at 11:00
  • The question is not about the number of if-else statements, but about the number of conditions inside an if () – holographic-principle Apr 18 '13 at 11:19
  • @finishingmove may be I misread it I have updated the answer for conditions too. – AurA Apr 18 '13 at 11:28
  • @AbhishekUmrao In your case switch might not be useful.. because you are checking only one if condition.. – AurA Apr 18 '13 at 11:30
1

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.org/en-US/docs/JavaScript/Reference/Statements/switch

Darren
  • 68,902
  • 24
  • 138
  • 144