猜数字困难版(1-10000)

小游戏,通过提示每次猜高或猜低以及每次猜中的位数,10次内猜中1-10000的一个数。

javascript 复制代码
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>猜数字游戏困难版</title>
    <style>
        body {
            text-align: center;
            padding-top: 20px;
        }
    </style>
</head>
<body>
<h1>猜数字游戏困难版</h1>
<p>我已经想了一个1到10000之间的数字。你有10次机会猜它是什么。</p>
<label for="guessInput">输入你的猜测:</label>
<input type="number" id="guessInput" required min="1" max="10000">
<button onclick="checkGuess()">猜!</button>
<button onclick="location.reload()">再来一局!</button>
<p id="output"></p>
<ol id="history"></ol>
<script>
    const secretNumber = Math.floor(Math.random() * 10000) + 1;
    let attempts = 10;
    const history = document.getElementById('history');
    const guessInput = document.getElementById('guessInput');
    const output = document.getElementById('output');
    function checkGuess() {
        const userGuess = parseInt(guessInput.value, 10);
        if (isNaN(userGuess)) {
            output.textContent = '请输入有效的数字!';
            return;
        }
        if (attempts > 0) {
		    attempts--;
            const rightNum = countSameDigits(userGuess, secretNumber)
            const guessItem = document.createElement('li');
            if (userGuess === secretNumber) {
                output.textContent = '恭喜你!你猜对了!游戏胜利!';
                attempts = -1;
            } else if (userGuess < secretNumber) {
                output.textContent = '猜低了。再试一次。';
            } else if (userGuess > secretNumber) {
                output.textContent = '猜高了。再试一次。';
            }
            guessItem.textContent = `猜测: ${userGuess};${output.textContent};猜对了${rightNum}位数`;
            history.appendChild(guessItem);            
            if (attempts > 0) {
                output.textContent += ` 剩余次数: ${attempts}`;
            } else if (attempts == 0) {
                output.textContent = ' 游戏失败。正确答案是: ' + secretNumber;
            }
        }
    }
    function countSameDigits(a, b) {
        // 将数字转换为字符串
        const strA = a.toString();
        const strB = b.toString();
        // 确保两个字符串长度相同,较短的字符串前面补0
        const maxLength = Math.max(strA.length, strB.length);
        strA.padStart(maxLength, '0');
        strB.padStart(maxLength, '0');
        let count = 0; // 用于计数相同位上的相同数
        for (let i = 0; i < maxLength; i++) {
            if (strA[i] === strB[i]) {
                count++;
            }
        }
        return count;
    }
</script>
</body>
</html>
相关推荐
触底反弹1 小时前
一文搞懂 Tailwind CSS 弹性布局:从原理到实战
前端·css·html
To_OC2 小时前
LC 42 接雨水:暴力超时卡半天?前后缀数组一用就通了
javascript·算法·leetcode
小林ixn2 小时前
从 ??= 到 onKeyDown:一个 React 组件的“自我修养”
前端·javascript·react.js
এ慕ོ冬℘゜3 小时前
前端实战:jQuery 多条件联合搜索(标题模糊查询 + 日历时间段筛选)
前端·javascript·jquery
随心点儿3 小时前
js postMessage 实现跨域发送消息-应用示例
javascript·ecmascript·postmessage
kyriewen5 小时前
我让AI改一个bug——它偷偷动了5个我没让它碰的地方
前端·javascript·ai编程
小白巨白5 小时前
玫瑰花园管理系统:AI识病+3D可视化,一套面向中小型玫瑰种植园的数字化管理工具
css·人工智能·计算机视觉·html5
gis开发之家9 小时前
《Vue3 从入门到大神35篇》Vue3 源码详解(五):effect 依赖收集原理——track 与 trigger 是如何工作的?
javascript·typescript·前端框架·vue3·vue3源码
橘子星10 小时前
我一个前端切图仔,凭什么能在浏览器里跑大模型?
前端·javascript·前端框架
半个落月10 小时前
用 LangChain JS 做可控写作实验:理解温度参数、提示词与异步调用
javascript·人工智能·后端