生成一个数字并让用户尝试猜测的JavaScript程序.我的问题是输入



我有随机发生器工作,我的问题是获得用户的输入。我似乎不知道如何触发输入。我使用了"window。alert"以及console.log"但是什么也没发生。我是新手。

我的HTML:

<DOCTYPE html>
<html>
<body>
<h2> Guessing For Random Numbers! </h2>
<p> A random number between one (1) and ten (10) will be selected. Try to guess the 
result! Input your answer: </p>

<script src= "RandomNumberMaker.js">
</script>
</body>
</html>

这是我的JavaScript:

var number = window.alert("Please Enter A Number Between One and Ten: ");
var random = Math.floor(Math.random() * 10) + 1;
if (random == number)
{
document.write ("Your Guess Was Correct!");
document.write(random);
}else
{
document.write ("Your Guess Was Incorrect. The Correct Answer Was: " + random);
}

您可以使用prompt获取用户input作为警报类型

var number = window.prompt("Please Enter A Number Between One and Ten: ");
var random = Math.floor(Math.random() * 10) + 1;
if (random == number) {
document.write("Your Guess Was Correct! It is" + number);
document.write(random);
} else {
document.write("Your Guess Was Incorrect. The Correct Answer Was: " + random + " not " + number);
}
<h2> Guessing For Random Numbers! </h2>
<p> A random number between one (1) and ten (10) will be selected. Try to guess the result! Input your answer: </p>

最新更新