如何在多维数组中检查变量是否为真?

时间:2016-08-29 06:50:28

标签: javascript html arrays

我试图调用这些数组并检查整个数组是否为真(如[“cell1”,“cell2”,“cell3”])。我下面有我的HTML和JS。

这对于某人来说可能很容易修复,我只是在努力寻找任何有关它的东西以及如何修复它。

<tr>
            <td id="cell1" onclick="tic(this)"></td>
            <td id="cell2" onclick="tic(this)"></td>
            <td id="cell3" onclick="tic(this)"></td>
        </tr>

        <tr>
            <td id="cell4" onclick="tic(this)"></td>
            <td id="cell5" onclick="tic(this)"></td>
            <td id="cell6" onclick="tic(this)"></td>
        </tr>

        <tr>
            <td id="cell7" onclick="tic(this)"></td>
            <td id="cell8" onclick="tic(this)"></td>
            <td id="cell9" onclick="tic(this)"></td>
        </tr>

var win = [
            ["cell1", "cell2", "cell3"],
            ["cell1", "cell4", "cell7"],
            ["cell1", "cell5", "cell9"],
            ["cell2", "cell5", "cell8"],
            ["cell3", "cell6", "cell9"],
            ["cell3", "cell5", "cell7"],
            ["cell4", "cell5", "cell6"],
            ["cell7", "cell8", "cell9"]
        ];

        function tic(element) {
            element.textContent = "X";

            for (i = 0; i < win.length; i++) { 
                var thisWinningCombo = win[i];

                for (x = 0; x < thisWinningCombo.length; x++) { 
                    var cellId = thisWinningCombo[x];
                    var cellValue = document.getElementById(cellId).textContent;
                    console.log(cellId + ": " + cellValue);
                }

            }
        }

1 个答案:

答案 0 :(得分:0)

这是你想要实现的吗?如果找到其中一个获胜组合,则hasWon变量设置为true

var win = [
            ["cell1", "cell2", "cell3"],
            ["cell1", "cell4", "cell7"],
            ["cell1", "cell5", "cell9"],
            ["cell2", "cell5", "cell8"],
            ["cell3", "cell6", "cell9"],
            ["cell3", "cell5", "cell7"],
            ["cell4", "cell5", "cell6"],
            ["cell7", "cell8", "cell9"]
        ];

        function tic(element) {
            element.textContent = "X";

            for (i = 0; i < win.length; i++) { 
                var thisWinningCombo = win[i];
                var hasWon = true;
                for (x = 0; x < thisWinningCombo.length; x++) { 
                    var cellId = thisWinningCombo[x];
                    var cellValue = document.getElementById(cellId).textContent;
                    //console.log(cellId + ": " + cellValue);
                    hasWon = hasWon && (cellValue == "X");
                }
                if (hasWon){
                  console.log(hasWon);
                }

            }
        }
<table border="1" cellpadding="10">
<tr>
            <td id="cell1" onclick="tic(this)"></td>
            <td id="cell2" onclick="tic(this)"></td>
            <td id="cell3" onclick="tic(this)"></td>
        </tr>

        <tr>
            <td id="cell4" onclick="tic(this)"></td>
            <td id="cell5" onclick="tic(this)"></td>
            <td id="cell6" onclick="tic(this)"></td>
        </tr>

        <tr>
            <td id="cell7" onclick="tic(this)"></td>
            <td id="cell8" onclick="tic(this)"></td>
            <td id="cell9" onclick="tic(this)"></td>
        </tr>
</table>