作业指示:
在。部分中创建一个带有块的页面 文献。该脚本应包括以下内容:
创建一个名为whileTest()的函数。在函数内部,创建一个 变量命名数字,并为其指定1到10之间的值。创建 另一个名为answer的变量,并为其赋值为零。然后 创建一个while循环。创建将导致循环执行的代码 只要数字变量不等于答案变量。 在循环内部,将答案变量分配给a的返回值 提示对话框。提示将要求用户猜出一个数字 在1到10之间。循环将继续,直到正确答案为止 进入。循环退出后,使用警告对话框通知 用户正确的猜测。
HERE'我的代码:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Lab 4-1</title>
<script type="text/javascript">
<!--
function whileTest (){
var number = 1, 10;
var answer = 0;
}
while (number != answer) {
alert("Please enter a number between 1 and 10");
var answer = prompt("Please enter a number between 1 and 10.","");
}
if (answer==number)
alert("You have entered the correct guess!!");
</script>
</head>
答案 0 :(得分:0)
这是正确的代码,其中的注释指出了您的错误。
<script type="text/javascript">
function whileTest() {
var number = Math.floor((Math.random() * 10) + 1); // <- create a number between 1 and 10
var answer = 0;
while (number != answer) { // <- this part also belongs to the function "whileTest()"
// no need to call "alert" here
answer = prompt("Please enter a number between 1 and 10.",""); // no need to create a new variable here (remove that "var")
}
// the while loop stops if number and answer are equal, so there is no need to check that again
alert("You have entered the correct guess!!");
}
whileTest(); // call the function
</script>