This repository has been archived by the owner on Jan 24, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add quiz app practicing prototype and inheritance
- Loading branch information
1 parent
8b689a0
commit 081cd2f
Showing
6 changed files
with
736 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
//Create Questions | ||
var questions = [ | ||
new Question("Who was the first President of the United States?", [ "George Washington", "Thomas Jefferson" ], "George Washington"), | ||
new Question("What is the answer to the Ultimate Question of Life, the Universe, and Everything?", ["Pi","42"], "42") | ||
]; | ||
|
||
//Create Quiz | ||
var quiz = new Quiz(questions); | ||
|
||
//Display Quiz | ||
QuizUI.displayNext(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
<head lang="en"> | ||
<meta charset="UTF-8"> | ||
<title>Amazing Quiz</title> | ||
<link rel="stylesheet" type="text/css" href="style.css"> | ||
</head> | ||
<body> | ||
|
||
<div class="grid"> | ||
<div id="quiz" class="centered grid__col--8"> | ||
<h1>Awesome Quiz</h1> | ||
|
||
<h2 id="question" class="headline-secondary--grouped"></h2> | ||
<h3 id="score"></h3> | ||
|
||
<p id="choice0"></p> | ||
<button id="guess0" class="btn--default">Select Answer</button> | ||
|
||
<p id="choice1"></p> | ||
<button id="guess1" class="btn--default">Select Answer</button> | ||
|
||
<footer> | ||
<p id="progress">Question x of y</p> | ||
</footer> | ||
</div> | ||
</div> | ||
|
||
<script src="quiz.js"></script> | ||
<script src="question.js"></script> | ||
<script src="quiz_ui.js"></script> | ||
<script src="app.js"></script> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
function Question(text, choices, answer) { | ||
this.text = text; | ||
this.choices = choices; | ||
this.answer = answer; | ||
} | ||
|
||
Question.prototype.isCorrectAnswer = function (choice) { | ||
return this.answer === choice; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
function Quiz(questions) { | ||
this.score = 0; | ||
this.questions = questions; | ||
this.currentQuestionIndex = 0; | ||
} | ||
|
||
Quiz.prototype.guess = function(answer) { | ||
if(this.getCurrentQuestion().isCorrectAnswer(answer)) { | ||
this.score++; | ||
} | ||
this.currentQuestionIndex++; | ||
}; | ||
|
||
Quiz.prototype.getCurrentQuestion = function() { | ||
return this.questions[this.currentQuestionIndex]; | ||
}; | ||
|
||
Quiz.prototype.hasEnded = function() { | ||
return this.currentQuestionIndex >= this.questions.length; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
var QuizUI = { | ||
displayNext: function () { | ||
if (quiz.hasEnded()) { | ||
this.displayScore(); | ||
} else { | ||
this.displayQuestion(); | ||
this.displayChoices(); | ||
this.displayProgress(); | ||
} | ||
}, | ||
displayQuestion: function() { | ||
this.populateIdWithHTML("question", quiz.getCurrentQuestion().text); | ||
}, | ||
displayChoices: function() { | ||
var choices = quiz.getCurrentQuestion().choices; | ||
|
||
for(var i = 0; i < choices.length; i++) { | ||
this.populateIdWithHTML("choice" + i, choices[i]); | ||
this.guessHandler("guess" + i, choices[i]); | ||
} | ||
}, | ||
displayScore: function() { | ||
var gameOverHTML = "<h1>Game Over</h1>"; | ||
gameOverHTML += "<h2> Your score is: " + quiz.score + "</h2>"; | ||
this.populateIdWithHTML("quiz", gameOverHTML); | ||
}, | ||
|
||
populateIdWithHTML: function(id, text) { | ||
var element = document.getElementById(id); | ||
element.innerHTML = text; | ||
}, | ||
guessHandler: function(id, guess) { | ||
var button = document.getElementById(id); | ||
button.onclick = function() { | ||
quiz.guess(guess); | ||
QuizUI.displayNext(); | ||
} | ||
}, | ||
|
||
displayProgress: function() { | ||
var currentQuestionNumber = quiz.currentQuestionIndex + 1; | ||
this.populateIdWithHTML("progress", "Question " + currentQuestionNumber + " of " + quiz.questions.length); | ||
} | ||
}; |
Oops, something went wrong.