Я хочу добавить условие if, чтобы определить, прошли ли вы тест или нет, но я не знаю, как добавить его в код. Например, если вы ответите на вопросы 10 из 10, вы пройдете тест, иначе вы не пройдете тест.
Я искал его в Google и YouTube в течение 2 дней, но не нашел ответьте, пожалуйста ... Я надеюсь, что кто-то может помочь мне с этим.
Вот мой код:
вопрос. js
function Question(text, choices, answer) {
this.text = text;
this.choices = choices;
this.answer = answer;
}
Question.prototype.isCorrectAnswer = function(choice) {
return this.answer === choice;
}
kuisoner. js
function Kuisoner(questions) {
this.score = 0;
this.questions = questions;
this.questionIndex = 0;
}
Kuisoner.prototype.getQuestionIndex = function() {
return this.questions[this.questionIndex];
}
Kuisoner.prototype.guess = function(answer) {
if(this.getQuestionIndex().isCorrectAnswer(answer)) {
this.score++;
}
this.questionIndex++;
}
Kuisoner.prototype.isEnded = function() {
return this.questionIndex === this.questions.length;
}
function populate() {
if(kuisoner.isEnded()) {
showScores();
}
else {
// show question
var element = document.getElementById("question");
element.innerHTML = kuisoner.getQuestionIndex().text;
// show options
var choices = kuisoner.getQuestionIndex().choices;
for(var i = 0; i < choices.length; i++) {
var element = document.getElementById("choice" + i);
element.innerHTML = choices[i];
guess("btn" + i, choices[i]);
}
showProgress();
}
};
function guess(id, guess) {
var button = document.getElementById(id);
button.onclick = function() {
kuisoner.guess(guess);
populate();
}
};
function showProgress() {
var currentQuestionNumber = kuisoner.questionIndex + 1;
var element = document.getElementById("progress");
element.innerHTML = "Question " + currentQuestionNumber + " of " + kuisoner.questions.length;
};
function showScores() {
var gameOverHTML = "<h1>Result</h1>";
gameOverHTML += "<h2 id='score'> Jawaban Ya: " + kuisoner.score + "</h2>";
var element = document.getElementById("kuisoner");
element.innerHTML = gameOverHTML;
};