Use the floor () method and random () of the Math object to return a random number between 0-100
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>猜数字小游戏</title>
</head>
<body>
请输入数字: <input type="text" id="in1" placeholder="请输入0-100之间的数字" />
<button onclick="num()">猜数字</button>
<button onclick="show()">查看数字</button>
<script type="text/javascript">
//1-100之间的随机数
var random=Math.floor(Math.random()*101);
//次数:初始为0
var count=0;
//猜数字
function num(){
var in1=document.getElementById("in1").value;
var num1=parseInt(in1);
count++;
if(num1>random){
alert("你猜的数字较大!");
}else if(num1<random){
alert("你猜的数字较小!");
}
else if(num1==random){
alert("恭喜你答对了!");
document.write("你共用了"+count+"次答对")
}else{
alert("请输入数字");
}
}
//查看数字
function show(){
alert(random);
}
</script>
</body>
</html>