未捕获的ReferenceError:aresults未定义,但数组已定义

时间:2019-01-26 22:54:56

标签: javascript arrays referenceerror

我是JavaScript的初学者。

运行此代码时,在Chrome's console中收到此错误: Uncaught ReferenceError: aresults is not defined

aresultsarray。我以为我已在For loop创建array将要存储的整数的运行之前对其进行了初始化。但是Chrome似乎说没有array

完成的代码旨在创建用户定义的1到10之间的整数,并将其存储在数组中,然后计算数组中属于用户定义的目标和10之间的整数的数量。 / p>

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

谢谢。

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

1 个答案:

答案 0 :(得分:1)

您正在函数内部创建所有变量,并尝试在外部访问它们。使用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>