javascript新手到忍者可以';我不能让书的代码工作

javascript新手到忍者可以';我不能让书的代码工作,javascript,css,html,Javascript,Css,Html,我从SitePoint购买了一本电子书,看起来很合法,但当我阅读代码(一章一章)时,我在8章之后就被卡住了。到目前为止,代码已经按规定运行,但我已经删除并重新编写了四次代码,并且在我的一生中都无法了解上次更改中发生了什么,提交框被表单替换 我已经浏览了所有论坛,寻找可能对这本书的代码有问题的其他人,但这本书似乎太新了(Javascript:Knower to Ninja是这本书的主题),人们不会有类似的问题。我已经在书中发现了一些印刷错误,但我感觉我的代码有点错误 任何帮助都将不胜感激。我也无

我从SitePoint购买了一本电子书,看起来很合法,但当我阅读代码(一章一章)时,我在8章之后就被卡住了。到目前为止,代码已经按规定运行,但我已经删除并重新编写了四次代码,并且在我的一生中都无法了解上次更改中发生了什么,提交框被表单替换

我已经浏览了所有论坛,寻找可能对这本书的代码有问题的其他人,但这本书似乎太新了(Javascript:Knower to Ninja是这本书的主题),人们不会有类似的问题。我已经在书中发现了一些印刷错误,但我感觉我的代码有点错误

任何帮助都将不胜感激。我也无法在控制台中弹出任何错误


谢谢

没关系,我实际上找到了调用gameOver()两次的地方,在将第一个实例添加到checkAnswer函数后,我必须删除它

谢谢

//dom references//
var $question = document.getElementById("question");
var $score = document.getElementById("score");
var $feedback = document.getElementById("feedback");
var $start = document.getElementById("start");
var $form = document.getElementById("answer");

//view functio{ns
function update(element, content, klass) {
    var p = element.firstChild || document.createElement("p");
    p.textContent = content;
    element.appendChild(p);
    if(klass) {
        p.className = klass;
    }
}
quiz = {
"name":"Super Hero Name Quiz",
"description":"How many super heroes can you name?",
"question":"What is the real name of ",
"questions": [
    { "question": "Superman", "answer":"Clark Kent" },
    { "question": "Wonder Woman", "answer": "Diana Prince" },
    { "question": "Batman", "answer": "Bruce Wayne"}
]
};

//Event Listeners
$start.addEventListener("click", function () {
    play(quiz);
}, false);

function hide(element) {
    element.style.display = "none";
}
function show(element) {
    element.style.display = "block";
}
//play(quiz);
hide($form);
function play(quiz) {
    //hide button and show form
    hide($start);
    show($form);
    $form.addEventListener('submit', function(event) {
        event.preventDefault();
        check($form[0].value);
    }, false);
    var score = 0; //initialize score
    update($score, score);
    //main game loop
    var i = 0;
    chooseQuestion();
    function chooseQuestion() {
        var question = quiz.questions[i].question;
        ask(question);
    }
    //end of main game loop
    gameOver();
    function ask(question) {
        update($question,quiz.question + question);
        $form[0].value = "";
        $form[0].focus();
    }
    function check(answer) {
        if(answer ===quiz.questions[i].answer) {
            update($feedback,"Correct!","right");
            score++;
            update($score,score);
        }
        else {
            update($feedback,"Wrong!","wrong");
        }
        i++;
        if(i === quiz.questions.length) {
            gameOver();
        }
        else {
            chooseQuestion();
        }
    }
    function gameOver() {
        update($question,"Game Over, you scored " + score + " points");
        hide($form);
        show($start);
    }
}