gpt4 book ai didi

ninja 的 javascript 新手无法使书籍代码正常工作

转载 作者:行者123 更新时间:2023-11-28 08:14:52 25 4
gpt4 key购买 nike

我从 SitePoint 购买了一本看起来相当合法的电子书,但是当我研究代码(逐章)时,我在 8 章后就卡住了。到目前为止的代码已按规定工作,但我已经删除并重新编写了这段代码四次,但我终其一生都无法找出在提交框被表单替换的最后更改中发生了什么。

Codepen of related code http://codepen.io/anon/pen/RNezjb

//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);
}
}

我已经在论坛中查找了所有可能对这本书的代码有疑问的人,但是这本书似乎太新了(Javascript:Novice to Ninja 是大部头),人们无法拥有类似的东西问题。我已经在书中发现了一些打印错误,但我感觉我的代码中有问题。

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

谢谢

最佳答案

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

谢谢

关于ninja 的 javascript 新手无法使书籍代码正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29043748/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com