Webbserverprogrammering 1

Show sourcecode

The following files exists in this folder. Click to view.

ramverket/exercises/quiz/js/

dashboard.js
quiz.js

quiz.js

55 lines ASCII Windows (CRLF)
const questions = document.querySelectorAll(".question");
const prevQuestionBtn = document.querySelector("#prev_question");
const nextQuestionBtn = document.querySelector("#next_question");
const submitBtn = document.querySelector("#submit_quiz");

let questionIndex = 0;

nextQuestionBtn.addEventListener("click", () => {
  if (questionIndex == 0) {
    prevQuestionBtn.style.display = "flex";
  }

  if (questionIndex < questions.length - 1) {
    questions[questionIndex].style.display = "none";
    questionIndex++;
    questions[questionIndex].style.display = "flex";
  }

  if (questionIndex == questions.length - 1) {
    nextQuestionBtn.style.display = "none";
    submitBtn.style.display = "flex";
  }
});

prevQuestionBtn.addEventListener("click", () => {
  nextQuestionBtn.style.display = "flex";
  submitBtn.style.display = "none";
  if (questionIndex > 0) {
    questions[questionIndex].style.display = "none";
    questionIndex--;
    questions[questionIndex].style.display = "flex";
  }

  if (questionIndex == 0) {
    prevQuestionBtn.style.display = "none";
  }
});

prevQuestionBtn.style.display = "none";

if (questions.length == 1) {
  nextQuestionBtn.style.display = "none";
  submitBtn.style.display = "flex";
} else {
  nextQuestionBtn.style.display = "flex";
  submitBtn.style.display = "none";
}

for (let i = 0; i < questions.length; i++) {
  if (i == 0) {
    questions[i].style.display = "flex";
  } else {
    questions[i].style.display = "none";
  }
}