首页 > 解决方案 > 未捕获的引用错误:未定义结果,但已定义数组

问题描述

我是 JavaScript 的初学者。

当我运行此代码时,我在以下位置收到此错误Chrome's consoleUncaught ReferenceError: aresults is not defined

aresults是一个arrayFor loop我以为我在创建array将要运行的整数之前对其进行了初始化。但Chrome好像说没有array

完成的代码旨在创建一个用户定义的 1 到 10 之间的整数数量,并将它们存储在一个数组中,然后计算数组中介于用户定义的目标和 10 之间的整数的数量。

我在这里做错了什么?我该如何解决?

谢谢。

<!DOCTYPE html>
<head>
</head>

<body>

    <h2>Storyteller dice roller</h2>

    <!--User selects number of 10-sided dice to be rolled, and target number -->
    Number of dice: <input type="number" value="5" id="dice"><br>
    Difficulty: <input type="number" value="6" id="diff"><br>

    <!--Button executes the "roll" JavaScript function-->
    <button onclick="roll()">Roll</button>

    <script>

        function roll() {

            // initialize array to store results of "dice rolls"
            var aresults = [];

            // store user input of "Number of dice" as vdice variable
            var vdice = document.getElementById("dice").value;

            // store user input of "Difficulty" as vdiff variable
            var vdiff = document.getElementById("diff").value;

            // simulates rolling a number of 10-sided dice

            // for loop:
            // set vrolled to 0;
            // run loop if vrolled is less than dice;
            // increase vrolled by 1 each time loop is run
            // continue for as many times as there are "dice"
            for (vrolled = 0; vrolled <= vdice; vrolled++) {

                // Create a random number between 1 and 10
                var vresult=Math.floor(Math.random() * 10) + 1;

                // append the new dice roll result to the array
                aresults.push(vresult);

            }
        }

        // Display variable results in console
        console.log("Results array");
        console.log(aresults);
        console.log("Dice to be rolled");
        console.log(vdice);
        console.log("Roll result");
        console.log(vresult);
        console.log("Difficulty");
        console.log(vdiff);
        console.log("Dice rolled");
        console.log(vrolled);
    </script>
</body>

标签: javascriptarraysreferenceerror

解决方案


您正在函数内部创建所有变量并尝试在外部访问它们。使用 var 定义意味着该变量将在范围内的任何地方可用。这并不总是意味着它在任何地方都可用,即全局 定义范围之外的所有变量将解决问题,因为所有变量都将变为全局变量。

<!DOCTYPE html>
<head>
</head>

<body>

    <h2>Storyteller dice roller</h2>

    <!--User selects number of 10-sided dice to be rolled, and target number -->
    Number of dice: <input type="number" value="5" id="dice"><br>
    Difficulty: <input type="number" value="6" id="diff"><br>

    <!--Button executes the "roll" JavaScript function-->
    <button onclick="roll()">Roll</button>

    <script>
var aresults = [];
var vdice = document.getElementById("dice").value;
var vresult;
// store user input of "Difficulty" as vdiff variable
var vdiff = document.getElementById("diff").value;
        function roll() {

// initialize array to store results of "dice rolls"


// store user input of "Number of dice" as vdice variable


// simulates rolling a number of 10-sided dice

// for loop:
// set vrolled to 0;
// run loop if vrolled is less than dice;
// increase vrolled by 1 each time loop is run
// continue for as many times as there are "dice"
for (vrolled = 0; vrolled <= vdice; vrolled++) {

// Create a random number between 1 and 10
vresult=Math.floor(Math.random() * 10) + 1;

// append the new dice roll result to the array
aresults.push(vresult);

}
}

// Display variable results in console
console.log("Results array");
console.log(aresults);
console.log("Dice to be rolled");
console.log(vdice);
console.log("Roll result");
console.log(vresult);
console.log("Difficulty");
console.log(vdiff);
console.log("Dice rolled");
console.log(vrolled);
</script>
</body>


推荐阅读