输入一到五十的整数,共有十次机会,中间猜对终止循环。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script>
function getRandomIntInclusive(min, max) {
min = Math.ceil(min);
max = Math.floor(max);
return Math.floor(Math.random() * (max - min + 1)) + min; //含最大值,含最小值
}
var random = getRandomIntInclusive(1, 50);
alert('猜数,请输入一到五十的数');
var judge = true;
for (var i = 1; i <= 10; i++) {
if (judge) {
var num = prompt('这是第' + i + '次输入');
while (judge) {
if (num > random) {
alert('你猜大了');
break;
} else if (num < random) {
alert('你猜小了');
break;
} else {
alert('你猜对了');
judge = false;
}
}
} else {
break;
}
}
</script>
</head>
<body>
</body>
</html>