Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript &引用;“未定义文档”;_Javascript_Loops_Document - Fatal编程技术网

Javascript &引用;“未定义文档”;

Javascript &引用;“未定义文档”;,javascript,loops,document,Javascript,Loops,Document,我是JavaScript新手,我很难解决这个问题。多谢各位 var attempts = 0; var randomNumber; while (randomNumber === 10) { randomNumber = (Math.random(10 - 1) + 1); attempts = attempts + 1; } document.write("It took " + attempts + " attempts"); 由于您正在调用document对象,因此代

我是JavaScript新手,我很难解决这个问题。多谢各位

var attempts = 0;
var randomNumber;

while (randomNumber === 10) {
    randomNumber = (Math.random(10 - 1) + 1);
    attempts = attempts + 1;
}

document.write("It took " + attempts + " attempts");

由于您正在调用
document
对象,因此代码应该包含在HTML代码段中。文档对象表示您的网页。如果要访问HTML页面中的任何元素,请始终从访问文档对象开始

例如:

<!DOCTYPE html>
<html>
<body>

<script>
var attempts = 0;
var randomNumber;

while (randomNumber === 10) {
    randomNumber = (Math.random(10 - 1) + 1);
    attempts = attempts + 1;
}

document.write("It took " + attempts + " attempts");
</script>

</body>
</html>

var=0;
var随机数;
而(随机数===10){
随机数=(数学随机数(10-1)+1);
尝试次数=尝试次数+1;
}
文件。写入(“需要”+尝试+”尝试);

其工作原理为何未定义?你想做什么?非常感谢!作为一个新手,这给了我很多非常有用的信息,我可以应用到其他事情上。谢谢