1

In this function, right after the first for loop, code isn't being reached. The alert("can't get here"); isn't working. Am I missing some obvious JavaScript caveat here? The rest of the code can be found here: http://jsbin.com/tiweniludoqe/7/edit Any help is much appreciated.

function checkForWin(){

    var winCondition = 0;

    //check for horizontal wins
    for(i = 0; i < board[i].length; i++) {
        for(j = 0; j < board[i].length; j++) {
            winCondition += board[i][j];
            if(winCondition === board[i].length) {
                alert("win detected horizontal");
            }
        }
        winCondition = 0;
    }
    alert("can't get here");

    //check for vertical wins
    for(i = 0; i < board[i].length; i++) {
        for(j = 0; j < board[i].length; j++) {
            winCondition += board[j][i];
            alert("winCondition: " + winCondition);
            if(winCondition === board[i].length) {
                alert("win detected horizontal");
            }
        }
    }

    //if diagonal / is 3 or -3 win
    //if diagonal \ is 3 or -3 win
}
4
  • Do you see any errors in your console? Edit: There is an uncaught error: Cannot read property 'length' of undefined Commented Sep 14, 2014 at 22:47
  • 5
    well your first loop condition should be board.length instead of board[i].length Commented Sep 14, 2014 at 22:48
  • Isn't board[i] an array as well though? That is, an array inside an array. Commented Sep 14, 2014 at 22:52
  • Declare i and j with var and use console.log() instead of alert() Commented Sep 14, 2014 at 22:56

1 Answer 1

1

I think that your code is falling into an infinite loop by checking for board[i].length in the first for loop.

Try this:

function checkForWin(){

    var winCondition = 0;

    //check for horizontal wins
    for(i = 0; i < board.length; i++) { //HERE WE CHECK FOR board.length INSTEAD OF board[i].length
        for(j = 0; j < board[i].length; j++) {
            winCondition += board[i][j];
            if(winCondition === board[i].length) {
                alert("win detected horizontal");
            }
        }
        winCondition = 0;
    }
    alert("can't get here");

    //check for vertical wins
    for(i = 0; i < board.length; i++) { //HERE YOU HAD THE SAME PROBLEM
        for(j = 0; j < board[i].length; j++) {
            winCondition += board[j][i];
            alert("winCondition: " + winCondition);
            if(winCondition === board[i].length) {
                alert("win detected horizontal");
            }
        }
    }

    //if diagonal / is 3 or -3 win
    //if diagonal \ is 3 or -3 win
}

Final recomendation... When iterating with for loops try to user the var for the iterating variable, like this:

for(var i=0; i < board.length; i++)

Instead of:

for(i=0; i < board.length; i++)

By doing this you are avoiding to populate the global scope with those variables (in the case above i).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.