Why does this loop, which is part of a Minimax statement, fail with the `if` condition added?

I'm working on a Connect Four game with Minimax: https://codepen.io/acchang/pen/XWePpWB

I'm stuck on the first portion of the Minimax, which is to build a function that would prioritize moves that result in three tokens in a row.

I think I’m almost there, but I can’t figure out why the loop in function scorePositionHoriz() is shutting down on me after just evaluating the first position.

It works when I comment out the if condition and loops through the entire array parallelBoard. But when I uncomment the if, the javascript console shows me the loop shuts down after one attempt.

Below are the two critical functions. Can you say why the for loop is shutting down with the if condition and is there another way to do it?

function scorePositionHoriz (board, player) {
    for (let r=0; r<6; r++) {
        for (let c=0; c<4; c++) {
            (console.log("checking: " + (board[r][c]),(board[r][c+1]),(board[r][c+2])))
            // if ((board[r][c] == player) && (board[r][c+1] == player) && (board[r][c+2] == player)) {
            //     score = score + 10
            //     console.log("scored is" + score)
            //     return score
            // }
            // else (console.log("no matches: " + (board[r][c]),(board[r][c+1]),(board[r][c+2])))
            // return 0

            // also tried:
            // if ((Number.isInteger(board[r][c]) == Number.isInteger(board[r][c+1])) &&
            // (Number.isInteger(board[r][c+1]) == Number.isInteger(board[r][c+2]))) {
            //     console.log("equal true")}
            //  else (console.log("no matches: " + (board[r][c]),(board[r][c+1]),(board[r][c+2])))
            // return 0

            // something about "if" throwing things off
        }
    }
};

function pickBestMove() {
    let bestScore = -1
    let bestColumn 
    let parallelAvailable = findAvailableSpots(parallelBoard)

     for (s=0; s<parallelAvailable.length; s++) {
            let i;
            let j = parseInt(parallelAvailable[s] - 1)
            console.log ("from avail spot " + j)
            for (i = 5; i > -1; i--) 
                if (Number.isInteger(parallelBoard[i][j])) {
                parallelBoard[i].splice((j), 1, whosPlaying())
                break
            }
        let positionScore = scorePositionHoriz (parallelBoard, whosPlaying())
        console.log("test board with marker in " + gameboard[i][j])
        parallelBoard[i].splice((j), 1, gameboard[i][j])

        if (positionScore > bestScore) {
         bestScore = positionScore
        console.log("Best Score is " + bestScore)
        bestColumn = s
        }
        else {console.log("not better")}
        console.log("tested avail spot: " + s)
        }
        console.log("BestColumn decided: " + bestColumn)
        if (bestColumn == 0){
            let altSpot = (availableSpots[Math.floor(Math.random() * availableSpots.length)] - 1)
            console.log("random choice:" + altSpot)
            return altSpot
        }
    else {return bestColumn}
}```

It's nothing to do with the if; it's the unconditional return 0; statement after the else block.

When your code reaches the first iteration of the two for loops, it either executes return score; or return 0;. That exits the current function, and no further iterations of your loops will be performed.

Reformatting your code should make it clearer:

if ((board[r][c] == player) && (board[r][c+1] == player) && (board[r][c+2] == player)) {
    score = score + 10
    console.log("scored is" + score)
    return score; // Function exits here; loops are terminated
}
else {
    (console.log("no matches: " + (board[r][c]),(board[r][c+1]),(board[r][c+2])));
}

return 0; // Function exits here; loops are terminated