mralasic

scriptjs

Nov 5th, 2021 (edited)
343
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const numQuestions = document.querySelectorAll(".questionContainer").length;
  2. let correctAnswers = 0;
  3.  
  4. document.getElementById("check").addEventListener("click", function(){
  5.   correctAnswers = 0;
  6.   for (let i = 1; i <=numQuestions;i++){
  7.     document.querySelector("#question" + i).classList.remove("incorrect");
  8.     document.querySelector("#question" + i).classList.remove("correct");
  9.   }
  10.  
  11.   for (let i = 1; i <= numQuestions; i++){
  12.     for (let j = 1; j <= 3; j++) {
  13.       if (document.getElementById("q" + i + "-ans" + j).checked && document.getElementById("q" + i + "-ans" + j).classList.contains("correct")){
  14.         correctAnswers++;
  15.         document.querySelector("#question" + i).classList.remove("incorrect");
  16.         document.querySelector("#question" + i).classList.add("correct");
  17.         break;
  18.       }
  19.       else {
  20.         document.querySelector("#question" + i).classList.add("incorrect");
  21.       }
  22.     }
  23.   }
  24.  
  25.   document.getElementById("result").textContent = "Correct answers " + correctAnswers + "/" + numQuestions;
  26. })
Add Comment
Please, Sign In to add comment