You need to enable JavaScript to run this app. JavaScript改写Python单词游戏时函数传参及判断、计数逻辑异常问题
优惠活动
大模型
产品
解决方案
定价
更多

JavaScript改写Python单词游戏时函数传参及判断、计数逻辑异常问题

问题根因
  • 所有输入都判定正确的原因:
    1. newWord函数中先执行words.splice(random, 1)删除选中的单词,再取words[random]传给输入框创建函数,此时该下标对应元素已被删除,传递的正确单词实际为undefined
    2. 调用checkAnswer时传参错误:仅传入了input DOM元素,没有传入正确单词,也没有获取input的输入值(.value)进行比对,判断逻辑完全失效
  • 正确计数始终为1的原因:你写的wordsCorrectCounter = +1是直接给变量赋值为1,不是累加计数,正确写法为wordsCorrectCounter += 1或者wordsCorrectCounter++
修复后的完整代码

JavaScript代码

function sleep(ms) {
    return new Promise(resolve => setTimeout(resolve, ms));
}

var words = ["meget", "går", "mor"];
var wordsWrong = [];
var wordsCorrectCounter = 0;
var wordsWrongCounter = 0;

function textToAudio(content) {
    let msg = content;
    let speech = new SpeechSynthesisUtterance();
    speech.lang = "da-DK";
    speech.text = msg;
    speech.volume = 1;
    speech.rate = 0.5;
    speech.pitch = 1;
    window.speechSynthesis.speak(speech);
}

async function newWord(words) {
    if (words.length === 0) {
        endGame();
    } else {
        var randomIndex = Math.floor(Math.random() * words.length);
        var correctWord = words[randomIndex];
        document.getElementById("Empty").innerHTML = correctWord;
        textToAudio(correctWord);
        await sleep(2000);
        textToAudio(correctWord);
        words.splice(randomIndex, 1);
        document.getElementById("Empty").innerHTML = "       ";
        createInputField(correctWord);
    }
};

function createInputField(correctWord) {
    var answerInput = document.createElement("input");
    answerInput.setAttribute("type", "text");
    answerInput.id = "inputfield";
    document.body.appendChild(answerInput)
    let btn = document.createElement("button");
    btn.id = "okBtn"
    btn.innerHTML = "ok";
    btn.type = "submit";
    btn.name = "answerBtn";
    document.body.appendChild(btn);
    document.getElementById("okBtn").addEventListener("click", () => checkAnswer(correctWord, answerInput.value))
}

function checkAnswer(correctWord, userAnswer) {
    if (userAnswer === correctWord) {
        console.log("correct");
        wordsCorrectCounter += 1;
        console.log(wordsCorrectCounter)
        document.getElementById("okBtn").remove();
        document.getElementById("inputfield").remove();
        newWord(words);
    } else {
        console.log("wrong");
        wordsWrongCounter += 1;
        wordsWrong.push(correctWord);
        console.log(wordsWrongCounter)
        document.getElementById("okBtn").remove();
        document.getElementById("inputfield").remove();
        newWord(words);
    }
};

document.getElementById("startGame").addEventListener("click", () => newWord(words));

function endGame() {
    document.getElementById("Empty").innerHTML = "you are done!" + "Correct: " + wordsCorrectCounter + " Wrong: " + wordsWrongCounter;
};

HTML代码

<!DOCTYPE html>
<html>
<head>
    <title>Aliyah's dictee spel</title>
    <link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
    <div id="header">
    <h1>Hej! Velkommen til Aliyahs diktee!</h1>
    </div>
    <div id="Random_word">
        <h2 id="Empty">Click start to start</h2>
        <button id="startGame">Start</button>
    </div>
</body>
</html>

内容的提问来源于stack exchange,提问作者Osman P.

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.22 03:27:03