Jack
Jack

Reputation: 67

why for loop is executing infinite times

 window.TicTacToet.compMove = function (row, col) {
     var player = window.TicTacToet.PlayerTurn;
     var board = window.TicTacToet.Board;
     for (i = 0; i < window.TicTacToet.Board.length; i++) {
         for (j = 0; j < window.TicTacToet.Board[i].length; j++) {
             if (window.TicTacToet.Board[i][j] == null) {
                 getWin(row, col, player, board);
             } else {
                 console.log("position occupied");
             }
         }
     }
 }

 function getWin($r, $c, $player, $board) {
     checkTop($r, $c, $player, $board);
 }

 function checkTop($x, $y, $player, b) {
     console.log("ENTER");
     var success = false;
     for (i = 0; i < 3; i++) {
         $x--;
         if ($x < 0) {
             return success;
         }
         if (b[$y][$x] != $player) {
             return success;
         }
     }
     success = true;
     return success;
 }

The function check-Top is executing infinite times. The parameters row and col are co-ordinates of the table. Player will return true or false and the board is an array having 9 elements. Both window.TicTacToet.Board.length and window.TicTacToet.Board[i].length have value 9 , i.e 9 x 9 .The console.log("ENTER") should execute 91 times but it is executing infinite times.what may be the reason for this. Because of this all other functions are not working properly, game itself not playing.Please Help out. This is for 9 x 9 clickable board drawing game.

Upvotes: 2

Views: 160

Answers (1)

naota
naota

Reputation: 4718

I guess you might want to use var keyword for variable i, because you are using same variable name i at two for loops. So, at the second for loop, you are unintentionally overwriting i of the first for loop. To avoid this, you can declare variables with var keyword, which defines variable scope.

Change

  for(i=0;i<window.TicTacToet.Board.length;i++)

To

  for(var i=0;i<window.TicTacToet.Board.length;i++)


And Change

  for (i=0;i<3;i++) 

To

  for (var i=0;i<3;i++)



JavaScript has function-level scope. When you declare variables within a function, they are only accessible within that function. The code below explains how variable scope works in JavaScript:

Without var keyword.

i = 100;
function foo(){
    i = 0; // overwriting i to 0
}
foo();
alert(i); // shows 0

With var keyword.

var i = 100;
function foo(){
    var i = 0; // This defines a new variable 'i' in the scope of function foo(){}.
}
foo();
alert(i); // shows 100

With var keyword ( in nested functions )

var i = 100;
function foo(){
    var i = 200; // A new variable 'i' in the scope of function foo(){}.
    function bar(){
        var i = 0;// A new variable 'i' in the scope of function bar(){}.
    }
    bar();
    alert(i); // shows 200
}
foo();
alert(i); //shows 100

In most languages which have block-level variable scope, variable are accessible whithin their block surrounded by curly brackets ({and}). But JavaSciprt doesn't terminate scopes at the end of blocks, but terminate them at the end of functions.

I'm sure there are many articles and documents about it. I googled it and found an intresting introductory article. http://javascriptissexy.com/javascript-variable-scope-and-hoisting-explained/

Hope this helps.

Upvotes: 6

Related Questions