So I am working on the next chapter of this book and again running into issues. I have added a form to the html and removed the alert prompts. When making all the adjustments for this chapter the questions are not being asked. I have tried to figure out this for a few hours and not moving forward at all.
Current issues I am running into. My form is not showing up to ask questions. It goes straight to the gameOver function and shows me a score of 0. The start button is not disapearing when I start the quiz.
Code:
HTML
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="description" content="A quiz game for ninjas">
<meta name="author" content="DAZ">
<title>Quiz Ninja</title>
<link rel="stylesheet" href="css/styles.css">
</head>
<body>
<header>
<h1>Quiz Ninja!</h1>
<p>Score: <strong id="score">0</strong></p>
</header>
<section id="question"></section>
<form id="answer">
<input type="text">
<button>Submit Answer</button>
</form>
<section id="feedback"></section>
<button id="start">Click To Play Quiz Ninja</button>
<script src="js/scripts.js"></script>
</body>
</html>
JS
//// 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 functions ////
function update(element, content, klass) {
var p = element.firstChild || document.createElement("p");
p.textContent = content;
element.appendChild(p);
if(klass) {
p.className = klass;
}
}
//// Event Listeners////
$start.addEventListener('click', function() { play(quiz) } , false);
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"}
]
}
var score = 0; // initialize score
function hide(element) {
element.style.display = "none";
}
function show(element) {
element.style.display = "block";
}
//hide the form at the start of the game
hide($form);
function play(quiz) {
//hide button and show form
hide($start);
show($form);
//main game loop
update($score,score);
$form.addEventListener('submit', function(event) {
event.preventDefault();
check($form[0].value);
}, false);
var i = 0;
chooseQuestion();
//end of main game loop
gameOver();
function chooseQuestion() {
var question = quiz.questions[i].question;
ask(question);
}
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");
//increase score by 1
score++;
update($score,score)
} else {
update ($feedback, "Wrong!", "wrong");
}
i++;
if(i===quiz.questions.length) {
gameOver();
} else {
chooseQuestion();
}
}
function gameOver() {
//inform the player that the game has finished and tell them how many points they have scored
update($question , "Game Over, you scored " + score + " points");
hide($form);
show($start);
}
}
CSS
*{
margin: 0;
padding: 0;
}
body {
width: 400px;
margin: 0 auto;
background: #fff;
height: 100vh;
}
html {
background: #444;
}
header {
font: bold 36px/120% Arial, Helvetica, sans-serif;
background: #333;
color: #c00;
text-transform: uppercase;
}
section p {
font: bold 24px/150% Arial, Helvetica, sans-serif;
background: #ccc;
border: #999 2px solid ;
color: #666;
text-align: center;
padding: 10px;
margin: 10px;
width: 300px;
}
.right {
background: #0c0;
border: #090 2px solid;
color: #600;
}
.wrong {
background:
}
input, button {
font: bold 24px/150% Arial, Helvetica, sans-serif;
display: block;
width: 300px;
padding: 10px;
margin: 10px auto;
}