Skip to content

Commit c5ab274

Browse files
authored
Added triggerAnswer() function
1 parent 323b53d commit c5ab274

File tree

1 file changed

+28
-25
lines changed

1 file changed

+28
-25
lines changed

src/trivia.js

Lines changed: 28 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -41,17 +41,17 @@ var trivia = {
4141
});
4242
return cats;
4343
},
44-
gotoNextQuestion: function(){ //this just forwards a "deprecated function"
44+
gotoNextQuestion: function () { //this just forwards a "deprecated function"
4545
displayQuestion();
4646
},
4747
insertCategoriesInfo: function () {
4848
var cats = this.getCategories();
4949
var unfcats = this.getUnfinishedCategories();
5050
$('#category-set').html('');
51-
cats.forEach((c)=> {
51+
cats.forEach((c) => {
5252
var $catbtn = $(`<button class="category-btn">${c}</button>`);
5353
if (unfcats.includes(c)) {
54-
$catbtn.on('click', function(e) {
54+
$catbtn.on('click', function (e) {
5555
trivia.currentCategory = c;
5656
onClickedCategory();
5757
});
@@ -64,7 +64,7 @@ var trivia = {
6464
trivia.state = "question";
6565
$(".answer-btn").attr("disabled", null);
6666
trivia.questionIndex = trivia.questions.findIndex((q) => {
67-
if(!this.categoriesEnabled) return !q.response;
67+
if (!this.categoriesEnabled) return !q.response;
6868
else return !q.response && q.category == this.currentCategory;
6969
});
7070
if (trivia.questions[trivia.questionIndex]) {
@@ -74,11 +74,11 @@ var trivia = {
7474
}
7575
}
7676
else {
77-
if(this.totalAnswered == this.totalQuestions) {
77+
if (this.totalAnswered == this.totalQuestions) {
7878
trivia.state = "thankyou";
7979
displayThankyou(); //game over
8080
}
81-
else if(this.categoriesEnabled) {
81+
else if (this.categoriesEnabled) {
8282
trivia.state = "categories";
8383
displayCategories();
8484
}
@@ -92,30 +92,33 @@ var trivia = {
9292
//listen for answer button clicks
9393
$(".screen").on("click", ".answer-btn", ev => {
9494
$(".answer-btn").attr("disabled", "disabled"); //turn off buttons to prohibit cheating :)
95-
if ($(ev.target).is("#correctAnswer")) {
96-
trivia.currentQuestion.response = "correct";
97-
trivia.state = "correct";
98-
} else {
99-
trivia.currentQuestion.response = "incorrect";
100-
trivia.state = "incorrect";
101-
}
102-
trivia.totalCorrect = trivia.questions.filter(item => {
103-
return item.response == "correct";
104-
}).length;
105-
trivia.totalAnswered = trivia.questions.filter(item => {
106-
return item.response;
107-
}).length;
108-
onClickedAnswer(trivia.currentQuestion.response == "correct");
95+
trivia.triggerAnswer($(ev.target).is("#correctAnswer"));
10996
});
11097

11198
//listen for restart button click
11299
$(".screen").on("click", ".start-btn", ev => {
113-
this.questions.forEach(function(q){ delete q.response });
114-
trivia.questionIndex = 0;
115-
if (!this.categoriesEnabled) trivia.state = "question";
116-
else trivia.state = "categories";
117-
trivia.currentQuestion = trivia.questions[0]; //reset to the first question
100+
this.questions.forEach(function (q) { delete q.response });
101+
trivia.questionIndex = 0;
102+
if (!this.categoriesEnabled) trivia.state = "question";
103+
else trivia.state = "categories";
104+
trivia.currentQuestion = trivia.questions[0]; //reset to the first question
118105
onClickedStart();
119106
});
107+
},
108+
triggerAnswer: function (correct) {
109+
if (correct) {
110+
trivia.currentQuestion.response = "correct";
111+
trivia.state = "correct";
112+
} else {
113+
trivia.currentQuestion.response = "incorrect";
114+
trivia.state = "incorrect";
115+
}
116+
trivia.totalCorrect = trivia.questions.filter(item => {
117+
return item.response == "correct";
118+
}).length;
119+
trivia.totalAnswered = trivia.questions.filter(item => {
120+
return item.response;
121+
}).length;
122+
onClickedAnswer(trivia.currentQuestion.response == "correct");
120123
}
121124
};

0 commit comments

Comments
 (0)