Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var currentQuestion = 0;
- var score = 0;
- var totQuestions = questions.length;
- var container = document.getElementById('quizContainer');
- var questionEl = document.getElementById('question');
- var opt1 = document.getElementById('opt1');
- var opt2 = document.getElementById('opt2');
- var opt3 = document.getElementById('opt3');
- var opt4 = document.getElementById('opt4');
- var nextButton = document.getElementById('nextButton');
- var resultCont = document.getElementById('result');
- var previousButton = document.getElementById('previousButton')
- var restartButton = document.getElementById('restartButton')
- var answerButton = document.getElementById('answerButton')
- function loadQuestion (questionIndex) {
- var q = questions[questionIndex];
- questionEl.textContent = (questionIndex + 1) + '. ' + q.question;
- opt1.textContent = q.option1;
- opt2.textContent = q.option2;
- opt3.textContent = q.option3;
- opt4.textContent = q.option4;
- };
- function loadpreviousQuestion () {
- var selectedOption = document.querySelector('input[type=radio]:checked');
- if(!selectedOption){
- alert('Please select your answer!');
- return;
- }
- var answer = selectedOption.value;
- /*if(questions[currentQuestion].answer == answer){
- score += 1;
- }*/
- selectedOption.checked = false;
- currentQuestion--;
- if(currentQuestion == totQuestions - 1){
- nextButton.textContent = 'Finish';
- }
- if(currentQuestion == totQuestions){
- container.style.display = 'none';
- resultCont.style.display = '';
- resultCont.textContent = 'Your Score: ' + score + ' out of 10';
- showAnswers();
- return;
- }
- loadQuestion(currentQuestion);
- }
- function loadNextQuestion () {
- var selectedOption = document.querySelector('input[type=radio]:checked');
- if(!selectedOption){
- alert('Please select your answer!');
- return;
- }
- var answer = selectedOption.value;
- if(questions[currentQuestion].answer == answer){
- score += 1;
- }
- selectedOption.checked = false;
- currentQuestion++;
- if(currentQuestion == totQuestions - 1){
- nextButton.textContent = 'Finish';
- }
- if(currentQuestion == totQuestions){
- container.style.display = 'none';
- resultCont.style.display = '';
- resultCont.textContent = 'Your Score: ' + score + ' out of 10';
- answerButton.style.display='block';
- //restartButton.style.display='block';
- //showAnswers();
- return;
- }
- loadQuestion(currentQuestion);
- }
- loadQuestion(currentQuestion);
- function resetquiz() {
- container.style.display = '';
- resultCont.style.display = 'none';
- score = 0;
- currentQuestion = 0;
- loadQuestion(currentQuestion)
- answerButton.style.display='none';
- nextButton.textContent = 'Next Question';
- }
- function showAnswers() {
- for(var i = 0; i < questions.length; i++) {
- var info = questions[i];
- var el = document.createElement('div');
- var questionTxt = document.createTextNode((i + 1 ) + '. ' + questions[i].question);
- var answer = ' ' + info['option' + info.answer];
- var answerEl = document.createElement('strong');
- var answerTxt = document.createTextNode(answer);
- answerEl.appendChild(answerTxt);
- el.appendChild(questionTxt);
- el.appendChild(answerEl);
- resultCont.appendChild(el);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement