// view functions ///
function update(element, content, klass) {
var p = element.firstChild || document.create.Element("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": "Clarke Kent"},
{ "question": "Batman", "answer": "Bruce Wayne"},
{ "question": "Wonder Woman", "answer": "Dianna Prince"}
]
}
//// dom references ////
var $question = document.getElementById("question");
var $score = document.getElementById("score");
var $feedback = document.getElementById("feedback");
var score = 0; //initialize score
update($score, score);
play(quiz);
function play(quiz) {
// main game loop
for (var i=0, question, answer, max=quiz.questions.length;i<max;i++) {
question = quiz.questions[i].question; //change is made here
answer = ask(question);
check(answer);
}
//end of game loop
gameOver();
function ask(question) {
update($question, quiz.question + question);
return prompt("Enter your answer:");
}
function check(answer) {
if(answer === quiz.questions[i].answer){
update($feedback, "Correct!", "right");
//increase score by 1
score ++;
update($score,score)
} else {
update($feedback, "Wrong!", "wrong");
}
}
function gameOver() {
// inform the player that the game is over and tell them
how many points they scored //
update($question, "Game Over, you scored " + score +"points");
}
}
}
Hello
I am trying to create a quiz that poses questions on what the superhero’s real name is, the answer, and user’s score using dynamic markup (DOM) so not using so many alert dialogs.
However, I cannot get the program to even run in the browser.
Someone please help diagnose the problem. Thanks very much.
That’s it - suggest replacing stone age tools with those that predate the dinosaurs.
document.write and document.writeln are as dead as Netscape 4. Don’t use them if the year you are living in is after 2004.
alert, confirm and prompt were repurposed for debugging use only in about 2006. They were replaced for that use several years ago once all browsers got a built in debugger by console.log. Before I turned alert off I aloways used to select the kill JavaScript checkbox when the alert appeeared on other people’s pages on the assumption that if they are asking if Ii wanted to turn JavaScript off (which alert does in some browsers) that I probably should.
It is sensible to have all of those long dead commands turned off in your browser as you don’t know what such antiquated scripts might do.
There are hundreds of DOM commands to choose from so that JavaScript can now rewrite your page in ways you can’t even imagine.