Javascript-未捕获的TypeError:string不是函数

萨钦

我在井字游戏网页的JavaScript代码方面遇到了一些麻烦。每当我移动时,控制台中都会发生此错误:Uncaught TypeError: string is not a function这是我的网页链接:http : //www.cgscomputing.com/36558/test.html这是代码:

    <h1>Tic Tac Toe</h1>

    <table>

        <tr>
            <td id = "spot1"></td>
            <td id = "spot2"></td>
            <td id = "spot3"></td>
        </tr>

        <tr>
            <td id = "spot4"></td>
            <td id = "spot5"></td>
            <td id = "spot6"></td>
        </tr>

        <tr>
            <td id = "spot7"></td>
            <td id = "spot8"></td>
            <td id = "spot9"></td>
        </tr>

    </table>

    <!---Contains the message telling which players' turn it currently is-->        
    <p id = "footer"></p>

    <script type = "text/javascript">

        //select a random number to decide which player goes first
        var randNum = Math.floor((Math.random() * 2));

        //list which contains what is printed to the document concerning the turn
        var beginTurn = ["Computer's ", "Your "];
        var turn = beginTurn[randNum];

        //print who's turn it is underneath the board
        var footer = document.getElementById("footer");
        footer.innerHTML = turn + " turn";

        //array containing all the possible combinations through which a player can win the game
        var possibleCombinations = [[2, 5, 8], [3, 5, 7], [6, 5, 4], [9, 5, 1], [1, 2, 3], [7, 8, 9], [1, 4, 7], [3, 6, 9]];

        //through the game, keeps track if there is a winner or not
        var won = false;

        //when true, the player will not be able to place another marker on the board, and will have to wait for the computer to put in a marker first
        var computerTurn = false;

        //function for the computer to find a spot to place its marker in the board
        function findLocation() {

            for (var n = 0; n < 8; n++) {

                //The computer first checks if it can win by placing one more insertMarker on the board

                if ((document.getElementById("spot" + possibleCombinations[n][0]).innerHTML == "O") && (document.getElementById("spot" + possibleCombinations[n][1]).innerHTML == "O") && (document.getElementById("spot" + possibleCombinations[n][2]).innerHTML == "")) {
                    return possibleCombinations[n][2];
                    break;
                }
                else if ((document.getElementById("spot" + possibleCombinations[n][0]).innerHTML == "O") && (document.getElementById("spot" + possibleCombinations[n][1]).innerHTML == "") && (document.getElementById("spot" + possibleCombinations[n][2]).innerHTML == "O")) {
                    return possibleCombinations[n][1];
                    break;
                }
                else if ((document.getElementById("spot" + possibleCombinations[n][0]).innerHTML == "") && (document.getElementById("spot" + possibleCombinations[n][1]).innerHTML == "O") && (document.getElementById("spot" + possibleCombinations[n][2]).innerHTML == "O")) {
                    return possibleCombinations[n][0];
                    break;
                }

                //If the computer cannot win, it checks if it can block the human player

                else if ((document.getElementById("spot" + possibleCombinations[n][0]).innerHTML == "X") && (document.getElementById("spot" + possibleCombinations[n][1]).innerHTML == "X") && (document.getElementById("spot" + possibleCombinations[n][2]).innerHTML == "")) {
                    return possibleCombinations[n][2];
                    break;
                }
                else if ((document.getElementById("spot" + possibleCombinations[n][0]).innerHTML == "X") && (document.getElementById("spot" + possibleCombinations[n][1]).innerHTML == "") && (document.getElementById("spot" + possibleCombinations[n][2]).innerHTML == "X")) {
                    return possibleCombinations[n][1];
                    break;
                }
                else if ((document.getElementById("spot" + possibleCombinations[n][0]).innerHTML == "") && (document.getElementById("spot" + possibleCombinations[n][1]).innerHTML == "X") && (document.getElementById("spot" + possibleCombinations[n][2]).innerHTML == "X")) {
                    return possibleCombinations[n][0];
                    break;
                }

            }

            //=======
            //If it cannot Win or Block, the compter chooses a random spot on the board to place a insertMarker on.

            //An empty array to contain all the avaliable spots on the board
            avaliableSpots = [];

            //The for loop adds all the avaliable spots from the board into the array

            for (var i = 1; i <= 9; i++) {
                var spot = "spot" + i;
                if (document.getElementById(spot).innerHTML == "") {
                    avaliableSpots.push(i);
                }
            }

            //A random number is generated and it is used to find a spot on the board from the avaliable spots contained in the array
            var randomSpot = Math.floor((Math.random() * (avaliableSpots.length)));
            return avaliableSpots[randomSpot];
        }

        //this function places the marker of the player and the computer on the board
        function insertMarker(insertMarker, spot) {

            if (won == false) {

                if (document.getElementById("spot" + spot).innerHTML == "") {

                    if (insertMarker == "X" && computerTurn == false) {

                        document.getElementById("spot" + spot).innerHTML = insertMarker;
                        footer.innerHTML = "Computer's turn";
                        turn = "Computer's ";
                        computerTurn = true;

                        //Sets a delay of 1 second before the computer places its marker
                        setTimeout(function(){
                            insertMarker("O", findLocation());
                            }, 1000);

                    } else if (insertMarker == "O") {

                        document.getElementById("spot" + spot).innerHTML = insertMarker;
                        computerTurn = false;
                        footer.innerHTML = "Your turn";
                        humanturn();

                    }

                    winner();
                }

            } 

        }

        //Function for the human player's turn. When the player selects a spot on the board, the insertMarker function is called, with the parameters X and the number of the spot.
        function humanturn() {
            //when the human player clicks on an empty spot, the insertMarker function is called with the parameters "x" and the number of the box
            document.getElementById("spot1").onclick = function() {insertMarker("X", 1)};
            document.getElementById("spot2").onclick = function() {insertMarker("X", 2)};
            document.getElementById("spot3").onclick = function() {insertMarker("X", 3)};
            document.getElementById("spot4").onclick = function() {insertMarker("X", 4)};
            document.getElementById("spot5").onclick = function() {insertMarker("X", 5)};
            document.getElementById("spot6").onclick = function() {insertMarker("X", 6)};
            document.getElementById("spot7").onclick = function() {insertMarker("X", 7)};
            document.getElementById("spot8").onclick = function() {insertMarker("X", 8)};
            document.getElementById("spot9").onclick = function() {insertMarker("X", 9)};
        }

        //This functions checks if there is a winner
        function winner() {

            for (var i = 0; i < 8; i++) {

                if ((document.getElementById("spot" + possibleCombinations[i][0]).innerHTML == "O") && (document.getElementById("spot" + possibleCombinations[i][1]).innerHTML == "O") && (document.getElementById("spot" + possibleCombinations[i][2]).innerHTML == "O")) {
                    footer.innerHTML = "COMPUTER WINS";
                    footer.style.color = "red";
                    document.getElementById("spot" + possibleCombinations[i][0]).style.backgroundColor = "yellow";
                    document.getElementById("spot" + possibleCombinations[i][1]).style.backgroundColor = "yellow";
                    document.getElementById("spot" + possibleCombinations[i][2]).style.backgroundColor = "yellow";
                    won = true;
                    break;
                }

                else if ((document.getElementById("spot" + possibleCombinations[i][0]).innerHTML == "X") && (document.getElementById("spot" + possibleCombinations[i][1]).innerHTML == "X") && (document.getElementById("spot" + possibleCombinations[i][2]).innerHTML == "X")) {
                    footer.innerHTML = "PLAYER WINS";
                    footer.style.color = "red";
                    document.getElementById("spot" + possibleCombinations[i][0]).style.backgroundColor = "yellow";
                    document.getElementById("spot" + possibleCombinations[i][1]).style.backgroundColor = "yellow";
                    document.getElementById("spot" + possibleCombinations[i][2]).style.backgroundColor = "yellow";
                    won = true;
                    break;

                }

            }

        }

        //If it is the computer's turn, the computer places a insertMarker using the insertMarker function
        if (turn == "Computer's ") {
            document.getElementById("footer").innerHTML = "Computer's turn";
            insertMarker("O", findLocation());
            turn = "Your ";
        }

        //If it is the human player's turn, the player places a insertMarker using the insertMarker function
        else if (turn == "Your ") {
            document.getElementById("footer").innerHTML = "Your turn";
            humanturn();
            turn = "Computer's ";
        }
    </script>
</body>

任何帮助都感激不尽。

斯图

您已经将该方法称为与参数相同的名称,因此,当您进行递归调用时,您正在调用的是“ O”或“ X”的参数-而不是函数。重命名其中之一,它应该可以解决此问题

编辑:应该说哪种方法。它是“ insertMarker”

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

未捕获的TypeError:undefined不是函数-Javascript

来自分类Dev

Javascript“未捕获的TypeError:对象不是函数”

来自分类Dev

JavaScript OOP:未捕获的TypeError:不是函数

来自分类Dev

Javascript:未捕获的TypeError:不是函数

来自分类Dev

JavaScript WordPress未捕获的typeerror $不是函数

来自分类Dev

未捕获的TypeError:ctx.fillText不是函数(Javascript画布)

来自分类常见问题

未捕获的TypeError:Object.values不是JavaScript函数

来自分类Dev

JavaScript关闭:未捕获的TypeError:counter.value不是函数

来自分类Dev

Javascript。未捕获的TypeError:Hashids不是构造函数

来自分类Dev

Javascript未捕获的TypeError:value.toUpperCase不是函数

来自分类Dev

Javascript中的“未捕获的TypeError:x.toUpperCase不是函数”

来自分类Dev

JavaScript错误:未捕获的TypeError:a [b]不是函数

来自分类Dev

Javascript regex .test()“未捕获的TypeError:未定义不是函数”

来自分类Dev

JavaScript代码块中的“未捕获的TypeError:未定义不是函数”

来自分类Dev

javascript [array] .splice无法正常工作(“未捕获的TypeError:collisions.splice不是函数”)

来自分类Dev

javascript-未捕获(承诺)TypeError:e.iterator不是函数。如何解决这种错误?

来自分类Dev

Javascript / Firestore:未捕获(承诺)TypeError:firebase.firestore(...)。collection(...)。doc(...)。collection(...)。set不是函数

来自分类Dev

jQuery / Javascript-未捕获的TypeError:未定义不是一个函数

来自分类Dev

JavaScript未捕获的Typeerror

来自分类Dev

在对象中调用方法时,Javascript中出现“未捕获的TypeError:未定义不是函数”的错误消息

来自分类Dev

JavaScript错误:未被捕获的TypeError:foo不是函数

来自分类Dev

未捕获的TypeError:$(…).on不是函数

来自分类Dev

未捕获的类型错误:未定义不是函数JavaScript模块

来自分类Dev

未捕获的类型错误:未定义不是函数JavaScript模块

来自分类Dev

JavaScript / jQuery“未捕获的TypeError” classList

来自分类Dev

JavaScript / jQuery“未捕获的TypeError” classList

来自分类Dev

从数据属性循环未捕获的TypeError的Javascript

来自分类Dev

未捕获到的TypeError:NaN JavaScript Sharepoint

来自分类Dev

未捕获的TypeError:非法调用javascript

Related 相关文章

  1. 1

    未捕获的TypeError:undefined不是函数-Javascript

  2. 2

    Javascript“未捕获的TypeError:对象不是函数”

  3. 3

    JavaScript OOP:未捕获的TypeError:不是函数

  4. 4

    Javascript:未捕获的TypeError:不是函数

  5. 5

    JavaScript WordPress未捕获的typeerror $不是函数

  6. 6

    未捕获的TypeError:ctx.fillText不是函数(Javascript画布)

  7. 7

    未捕获的TypeError:Object.values不是JavaScript函数

  8. 8

    JavaScript关闭:未捕获的TypeError:counter.value不是函数

  9. 9

    Javascript。未捕获的TypeError:Hashids不是构造函数

  10. 10

    Javascript未捕获的TypeError:value.toUpperCase不是函数

  11. 11

    Javascript中的“未捕获的TypeError:x.toUpperCase不是函数”

  12. 12

    JavaScript错误:未捕获的TypeError:a [b]不是函数

  13. 13

    Javascript regex .test()“未捕获的TypeError:未定义不是函数”

  14. 14

    JavaScript代码块中的“未捕获的TypeError:未定义不是函数”

  15. 15

    javascript [array] .splice无法正常工作(“未捕获的TypeError:collisions.splice不是函数”)

  16. 16

    javascript-未捕获(承诺)TypeError:e.iterator不是函数。如何解决这种错误?

  17. 17

    Javascript / Firestore:未捕获(承诺)TypeError:firebase.firestore(...)。collection(...)。doc(...)。collection(...)。set不是函数

  18. 18

    jQuery / Javascript-未捕获的TypeError:未定义不是一个函数

  19. 19

    JavaScript未捕获的Typeerror

  20. 20

    在对象中调用方法时,Javascript中出现“未捕获的TypeError:未定义不是函数”的错误消息

  21. 21

    JavaScript错误:未被捕获的TypeError:foo不是函数

  22. 22

    未捕获的TypeError:$(…).on不是函数

  23. 23

    未捕获的类型错误:未定义不是函数JavaScript模块

  24. 24

    未捕获的类型错误:未定义不是函数JavaScript模块

  25. 25

    JavaScript / jQuery“未捕获的TypeError” classList

  26. 26

    JavaScript / jQuery“未捕获的TypeError” classList

  27. 27

    从数据属性循环未捕获的TypeError的Javascript

  28. 28

    未捕获到的TypeError:NaN JavaScript Sharepoint

  29. 29

    未捕获的TypeError:非法调用javascript

热门标签

归档