我完全不熟悉JavaScript,可以真正使用一些帮助。我必须在JavaScript中创建一个程序,它随机生成1到12之间的两个数字并将它们存储到文本字段中。这两个数字是全局变量,并发送到一个函数,该函数将文本字段字符串显示为“x次y”(x和y是随机生成的数字)。它还应该有一个检查答案按钮,使用window.alert
将输入的答案与正确的答案进行比较,并根据他们给出的答案显示一条消息。我没有做太多的工作,所以我对缺乏代码表示歉意,但任何帮助对于课堂任务都非常有帮助。谢谢!JavaScript随机数学乘法生成器
var x, y;
function aNumber(x, y) {
Math.floor((Math.random() * 12) + 1);
Math.floor((Math.random() * 12) + 1);
return x, y;
}
function genQuestion(x, y) {
document.getElementById('question').value = aNumber(x) + "times" + aNumber(y);
}
<body>
<h1>Learning Multiplication</h1>
<form name="myForm" id="myForm" action="#">
<label>What is: </label>
<input id="question" name="question" type="text" />
<br>
<label>The answer is: </label>
<input id="answer" name="answer" type="text" />
<br>
<label>Correct answers: </label>
<input id="total" name="total" type="text" />
<br>
<button class = "button" name="button" type="button" value="Check answer" onclick="genQuestion()" />
</form>
</body>
提示:你的函数'aNumber的(X,Y)'返回被传递到函数的值相同。 Math.floor((Math.random()* 12)+ 1);'的结果不会被分配给任何变量。另外,我会考虑命名该函数,因为它返回2个值,而不是“aNumber”。 – lxxxvi