jeudi 9 avril 2020

I want to add if conditional to determine you pass the test or not, but i don't now how to add it in the code

I want to add if conditional to determine you pass the test or not, but i don't now how to add it in the code. For example if you answer 10/10 question you will pass it, and if not you will not pass the test.

I've search it in google and youtube for 2 day, but doesn't get the answer, please... i hope someone can help me

Here is my code:

question.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;
}

app.js

    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;
};

Aucun commentaire:

Enregistrer un commentaire