猜数字游戏,限制次数为10次

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>Documenttitle>
    <script>
        function getRandom(min, max) {
            min = Math.ceil(min);
            max = Math.floor(max);
            return Math.floor(Math.random() * (max - min + 1)) + min; //含最大值,含最小值 
        }
        var random = getRandom(1, 50);
        for (var i = 1; i <= 10; i++) {
            var num = prompt('猜一猜? 输入1-100之间的数字');
            if (num > random) {
                alert('你猜大了!');
            } else if (num < random) {
                alert('你猜小了!');
            } else {
                alert('恭喜你,猜到了!')
                break;
            }
            if (i == 10) {
                alert('你的次数已用完!')
            }

        }
    script>
head>

<body>

body>

html>

注意要在猜对数字后循环结束,写break退出循环

你可能感兴趣的:(笔记,html5,前端,javascript)