function Quiz(questions) { this.score = 0; this.questions = questions; this.questionIndex = 0; } Quiz.prototype.getQuestionIndex = function() { return this.questions[this.questionIndex]; } Quiz.prototype.guess = function(answer) { if(this.getQuestionIndex().isCorrectAnswer(answer)) { this.score++; } this.questionIndex++; } Quiz.prototype.isEnded = function() { return this.questionIndex === this.questions.length; } function Question(text, choices, answer) { this.text = text; this.choices = choices; this.answer = answer; } Question.prototype.isCorrectAnswer = function(choice) { return this.answer === choice; } function populate() { if(quiz.isEnded()) { showScores(); } else { // show question var element = document.getElementById("question"); element.innerHTML = quiz.getQuestionIndex().text; // show options var choices = quiz.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() { quiz.guess(guess); populate(); } }; function showProgress() { var currentQuestionNumber = quiz.questionIndex + 1; var element = document.getElementById("progress"); element.innerHTML = "السؤال " + currentQuestionNumber + " من " + quiz.questions.length; }; function showScores() { var gameOverHTML = "

النتيجة

"; gameOverHTML += "

نتيجتك : " + quiz.score + ' من ' + quiz.questions.length + ' ' + '
' + '
' + ' و الاجابات الصحيحة بالترتيب هي' + '
' + '
' + ' إجابة السؤال الأول : جنتاميسين' + '
' + ' إجابة السؤال الثاني : حب الشباب' + '
' + ' إجابة السؤال الثالث : متلازمة راي' + '
' + ' إجابة السؤال الرابع : كل ما سبق' + '
' + ' إجابة السؤال الخامس : مترونيدازول' + "

"; var element = document.getElementById("quiz"); element.innerHTML = gameOverHTML; }; // create questions var questions = [ new Question("أحد هذه المضادات الحيوية قد تسبب سمية كلوية و سمعية", ["سيفادروكسيل", "سيفوتاكسيم","جنتاميسين", "أموكسيسيلين"], "جنتاميسين"), new Question("تستعمل مادة بنزويل بيروكسيد في علاج", ["التينيا", "السكر", "الصرع", "حب الشباب"], "حب الشباب"), new Question("استخدام الاسبرين للاطفال قد يسبب متلازمة", ["داون", "زولينجر اليسون","ستيفن جونسون", "راي"], "راي"), new Question("تعتبر مادة فيوريسيميد المادة الفعالة في لازكس", ["مدر للبول", "علاج ارتفاع ضغط الدم", "كل ما سبق", "ليس أي مما سبق"], "كل ما سبق"), new Question("يسبب هذا الدواء الشعور بطعم معدن في الفم عند تعاطيه كما أنه لا يعطى للمرضى الذين يتناولون وارفارين لأنه يزيد تأثير السيولة", ["رانتيدين", "أزيثروميسين","أورليستات", "مترونيدازول"], "مترونيدازول") ]; // create quiz var quiz = new Quiz(questions); // display quiz populate();