Javascript program not working

// 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.

Hi vanillacream welcome to the forum

“doesn’t run in the browser” isn’t much information to go on.
What error messages are you seeing in the console?

i cannot get anything to show up besides the title. The questions (displayed in html) nor answer dialog box will show.

If you want to avoid using Alerts you can use document.writeln();

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.

i cannot get anything to show up besides the title. The questions (displayed in html) nor answer dialog box will show.

Open up the console in the browser like @Mittineague suggested and try and track down the errors

SyntaxError: missing ; before statement
how many points they scored //
^

Multi line comments don’t work with //

Second error, you’re missing a curly brace at the end of the play function

Third error document.create.Element("p") needs to be document.createElement("p")

Will they take me to strange new worlds?

1 Like

Strange new worlds that are beyond your wildest imagination

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.