Uncaught ReferenceError: aresults is not defined, but array was defined

Uncaught ReferenceError: aresults is not defined, but array was defined

我是 JavaScript 的初学者。

当我 运行 这段代码时,我在 Chrome's console 中得到这个错误: Uncaught ReferenceError: aresults is not defined

aresults 是一个 array。我以为我在创建整数的 For 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>

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

<!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>