Skip to content

Commit 25ac6d6

Browse files
author
Em01
committed
Basic quiz app
1 parent 551e6fd commit 25ac6d6

File tree

7 files changed

+737
-0
lines changed

7 files changed

+737
-0
lines changed

objectOriented/quiz/.DS_Store

6 KB
Binary file not shown.

objectOriented/quiz/Quiz/app.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
//Create Questions
2+
var questions = [
3+
new Question("Who was the first President of the United States?", [ "George Washington", "Thomas Jefferson" ], "George Washington"),
4+
new Question("What is the answer to the Ultimate Question of Life, the Universe, and Everything?", ["Pi","42"], "42")
5+
];
6+
7+
//Create Quiz
8+
var quiz = new Quiz(questions);
9+
10+
//Display Quiz
11+
QuizUI.displayNext();
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head lang="en">
4+
<meta charset="UTF-8">
5+
<title>Amazing Quiz</title>
6+
<link rel="stylesheet" type="text/css" href="style.css">
7+
</head>
8+
<body>
9+
10+
<div class="grid">
11+
<div id="quiz" class="centered grid__col--8">
12+
<h1>Awesome Quiz</h1>
13+
14+
<h2 id="question" class="headline-secondary--grouped"></h2>
15+
<h3 id="score"></h3>
16+
17+
<p id="choice0"></p>
18+
<button id="guess0" class="btn--default">Select Answer</button>
19+
20+
<p id="choice1"></p>
21+
<button id="guess1" class="btn--default">Select Answer</button>
22+
23+
<footer>
24+
<p id="progress">Question x of y</p>
25+
</footer>
26+
</div>
27+
</div>
28+
29+
<script src="quiz.js"></script>
30+
<script src="question.js"></script>
31+
<script src="quiz_ui.js"></script>
32+
<script src="app.js"></script>
33+
</body>
34+
</html>
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
function Question(title, choices, answer){
2+
Choices.call(this, title, answer);
3+
this.title = title;
4+
this.answer = answer;
5+
}
6+
7+
Question.prototype.isCorrectAnswer = function(choice) {
8+
return this.answer === choice;
9+
};

objectOriented/quiz/Quiz/quiz.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
function Quiz(questions) {
2+
this.score = 0;
3+
this.questions = questions;
4+
this.currentQuestionIndex = 0;
5+
}
6+
7+
Quiz.prototype.guess = function(answer) {
8+
if(this.getCurrentQuestion().isCorrectAnswer(answer)) {
9+
this.score++;
10+
}
11+
this.currentQuestionIndex++;
12+
};
13+
14+
Quiz.prototype.getCurrentQuestion = function() {
15+
return this.questions[this.currentQuestionIndex];
16+
};
17+
18+
Quiz.prototype.hasEnded = function() {
19+
return this.currentQuestionIndex >= this.questions.length;
20+
};
21+
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
var QuizUI = {
2+
displayNext: function () {
3+
if (quiz.hasEnded()) {
4+
this.displayScore();
5+
} else {
6+
this.displayQuestion();
7+
this.displayChoices();
8+
this.displayProgress();
9+
}
10+
},
11+
displayQuestion: function() {
12+
this.populateIdWithHTML("question", quiz.getCurrentQuestion().text);
13+
},
14+
displayChoices: function() {
15+
var choices = quiz.getCurrentQuestion().choices;
16+
17+
for(var i = 0; i < choices.length; i++) {
18+
this.populateIdWithHTML("choice" + i, choices[i]);
19+
this.guessHandler("guess" + i, choices[i]);
20+
}
21+
},
22+
displayScore: function() {
23+
var gameOverHTML = "<h1>Game Over</h1>";
24+
gameOverHTML += "<h2> Your score is: " + quiz.score + "</h2>";
25+
this.populateIdWithHTML("quiz", gameOverHTML);
26+
},
27+
28+
populateIdWithHTML: function(id, text) {
29+
var element = document.getElementById(id);
30+
element.innerHTML = text;
31+
},
32+
guessHandler: function(id, guess) {
33+
var button = document.getElementById(id);
34+
button.onclick = function() {
35+
quiz.guess(guess);
36+
QuizUI.displayNext();
37+
}
38+
},
39+
40+
displayProgress: function() {
41+
var currentQuestionNumber = quiz.currentQuestionIndex + 1;
42+
this.populateIdWithHTML("progress", "Question " + currentQuestionNumber + " of " + quiz.questions.length);
43+
}
44+
};

0 commit comments

Comments
 (0)