javascript - 未捕获的引用错误:未定义结果,但已定义数组
问题描述
我是 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 定义意味着该变量将在范围内的任何地方可用。这并不总是意味着它在任何地方都可用,即全局 定义范围之外的所有变量将解决问题,因为所有变量都将变为全局变量。
<!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>
推荐阅读
- symfony - 以前执行的迁移不是注册的迁移
- python - 如何从一个模块导入类并在 django 中初始化和运行它
- aws-step-functions - Step Function 状态后面的任何队列
- python - 读取文件内存对象的问题
- macos - 在 MacOS 上使用 AVFoudation 从摄像头捕获视频时内存泄漏
- javascript - Safari浏览器中页面/窗口滚动上的Div“闪烁”
- anylogic - 在货架系统中优先考虑托盘货架
- angular - Ionic-Framework (4) - Openlayers 地图不工作/不可见
- sql-server - 如何限制列中可以放置的值范围?
- matlab - 在 Matlab 中沿 2 个轴对矩阵进行高斯滤波