<style>

.quiz-container {

  font-family: "Roboto", sans-serif;

  max-width: 900px;

  margin: 0 auto;

  padding: 16px;

  display: none;

}

    .quiz-container .timer {

  font-weight: bold;

  text-align: right;

}

    .timer.danger {

  color: red;

}

    .quiz-container h2.question {

  font-size: 20px;

  background: #d0ecff;

  padding: 16px;

  border-radius: 8px;

  font-weight: normal;

  line-height: 1.6;

}

    .quiz-container .options {

  display: grid;

  grid-template-columns: 1fr 1fr;

  gap: 16px;

  margin-bottom: 20px;

}

    .quiz-container .option {

  border: none;

  padding: 24px 32px;

  font-size: 18px;

  background: #1d3557;

  color: #fff;

}

    .quiz-container button {

  cursor: pointer;

}

    .disabled {

  pointer-events: none;

}

    .option.correct {

  background: #51e351;

  color: #222;

}

    .option.incorrect {

  background: #e63946;

}

    .quiz-container .next-btn,

.quiz-result .retake-btn {

  background: #222;

  color: #fff;

  border: none;

  padding: 12px 32px;

  font-size: 20px;

  text-transform: uppercase;

  font-weight: bold;

  letter-spacing: 3px;

  cursor: pointer;

  width: fit-content;

}

    .quiz-result {

  display: none;

  flex-direction: column;

  gap: 24px;

  max-width: 900px;

  margin: 0 auto;

  font-family: "Roboto", sans-serif;

  padding: 16px;

}

    .quiz-result .question-container {

  padding: 12px;

  border: 1px solid #eee;

  display: flex;

  flex-direction: column;

  gap: 10px;

}

    .quiz-result .question-container.incorrect {

  background: #e63946;

  color: #fff;

}

    .question-number {

  font-size: 16px;

  margin-right: 16px;

  background: #1d3557;

  color: #fff;

  padding: 8px;

  border-radius: 8px;

}

    @media (max-width: 700px) {

  .quiz-container .options {

    grid-template-columns: 1fr;

  }

}

.start-btn-container h2 {

  font-size: 40px;

  margin: 0;

}


.start-btn-container .start-btn {

  background: #e63946;

  color: #fff;

  padding: 8px 32px;

  border-radius: 8px;

  border: none;

  font-size: 24px;

  cursor: pointer;

  text-transform: uppercase;

  font-weight: bold;

}

    

.start-btn-container {

  font-family: "Roboto", sans-serif;

  display: flex;

  flex-direction: column;

  align-items: center;

}



  </style>


  <div class="start-btn-container">

    <div class="separator" style="clear: both; text-align: center;"><div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgdT6UxhAw1AoL-qjapQYdvyZ9i_kSbJ9doc4X7NBzV6UIUrmapXHVLMPcaV1119YGPpNw5EMMIRFOUpH2WlClyp0WURkjaINPuAeikyIxK101-ldfw8uE4jfq-xCEruWWOkr92YbfhSiERhVee_2lovTQPFmgUz3JkttBoEXunBGhEsjKG1lJjGgk2HUc/s800/SD.png" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="400" data-original-width="800" height="160" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgdT6UxhAw1AoL-qjapQYdvyZ9i_kSbJ9doc4X7NBzV6UIUrmapXHVLMPcaV1119YGPpNw5EMMIRFOUpH2WlClyp0WURkjaINPuAeikyIxK101-ldfw8uE4jfq-xCEruWWOkr92YbfhSiERhVee_2lovTQPFmgUz3JkttBoEXunBGhEsjKG1lJjGgk2HUc/s320/SD.png" width="320" /></a></div><br /><br /></div><h2>Quiz</h2>

    <p>Materi To Be</p>

    <button class="start-btn">Start Quiz</button>

  </div>


  <div class="quiz-container">

    <h1>Quiz Bahasa Inggris</h1>

    <div class="timer">Time left: 10 seconds</div>

    <h2 class="question"></h2>

    <div class="options"></div>

    <button class="next-btn">Next</button> 

  </div>

  <div class="quiz-result">

</div>

<div class="quiz-result">

  <h2>Hasil Quiz</h2>

  <p id="quiz-score"></p>

  <script>

let quizData = [

  {

    question: "What is the correct form of 'to be' in the sentence: 'I _____ a teacher.'",

    options: ["am", "is", "are", "be"],

    correct: "am",

  },

  {

    question: "Choose the correct form of 'to be' to complete the sentence: 'They _____ from France.'",

    options: ["is", "am", "are", "be"],

    correct: "are",

  },

  {

    question: "Which sentence is correct?",

    options: ["She am happy.", "We is students.", "He are a doctor.", "They are my friends."],

    correct: "They are my friends.",

  },

  {

    question: "What is the negative form of 'I am a student'?",

    options: ["I not am a student.", "I am not a student.", "I be not a student.", "I is not a student."],

    correct: "I am not a student.",

  },

  {

    question: "Which of the following sentences is in the interrogative form?",

    options: ["He is a doctor.", "We are happy.", "Are you tired?", "They are studying."],

    correct: "Are you tired?",

  },

  {

    question: "What is the correct form of 'to be' in the sentence: 'She _____ my sister.'",

    options: ["is", "am", "are", "be"],

    correct: "is",

  },

  {

    question: "Complete the sentence: 'You _____ a good student.'",

    options: ["am", "is", "are", "be"],

    correct: "are",

  },

  {

    question: "Which sentence is correct?",

    options: ["It am a book.", "She are a teacher.", "They is my family.", "He is a doctor."],

    correct: "He is a doctor.",

  },

  {

    question: "What is the short form of 'I am not'?",

    options: ["I'm not", "I are not", "I is not", "I be not"],

    correct: "I'm not",

  },

  {

    question: "Which of the following is a negative sentence?",

    options: ["She is my friend.", "We are going to the park.", "He is not coming to the party.", "They are playing football."],

    correct: "He is not coming to the party.",

  }

];

const quizContainer = document.querySelector(".quiz-container");

const question = document.querySelector(".quiz-container .question");

const options = document.querySelector(".quiz-container .options");

const nextBtn = document.querySelector(".quiz-container .next-btn");

const quizResult = document.querySelector(".quiz-result");

const startBtnContainer = document.querySelector(".start-btn-container");

const startBtn = document.querySelector(".start-btn-container .start-btn");


let questionNumber = 0;

let score = 0;

const MAX_QUESTIONS = 10;

let timerInterval;


const shuffleArray = (array) => {

  return array.slice().sort(() => Math.random() - 0.5);

};


quizData = shuffleArray(quizData);


const resetLocalStorage = () => {

  for (i = 0; i < MAX_QUESTIONS; i++) {

    localStorage.removeItem(`userAnswer_${i}`);

  }

};


resetLocalStorage();


const checkAnswer = (e) => {

  let userAnswer = e.target.textContent;

  if (userAnswer === quizData[questionNumber].correct) {

    score++;

    e.target.classList.add("correct");

  } else {

    e.target.classList.add("incorrect");

  }


  localStorage.setItem(`userAnswer_${questionNumber}`, userAnswer);


  let allOptions = document.querySelectorAll(".quiz-container .option");

  allOptions.forEach((o) => {

    o.classList.add("disabled");

  });

};


const createQuestion = () => {

  clearInterval(timerInterval);


  let secondsLeft = 9;

  const timerDisplay = document.querySelector(".quiz-container .timer");

  timerDisplay.classList.remove("danger");


  timerDisplay.textContent = `Time Left: 10 seconds`;


  timerInterval = setInterval(() => {

    timerDisplay.textContent = `Time Left: ${secondsLeft

      .toString()

      .padStart(2, "0")} seconds`;

    secondsLeft--;


    if (secondsLeft < 3) {

      timerDisplay.classList.add("danger");

    }


    if (secondsLeft < 0) {

      clearInterval(timerInterval);

      displayNextQuestion();

    }

  }, 1000);


  options.innerHTML = "";

  question.innerHTML = `<span class='question-number'>${

    questionNumber + 1

  }/${MAX_QUESTIONS}</span>${quizData[questionNumber].question}`;


  const shuffledOptions = shuffleArray(quizData[questionNumber].options);


  shuffledOptions.forEach((o) => {

    const option = document.createElement("button");

    option.classList.add("option");

    option.innerHTML = o;

    option.addEventListener("click", (e) => {

      checkAnswer(e);

    });

    options.appendChild(option);

  });

};


const retakeQuiz = () => {

  questionNumber = 0;

  score = 0;

  quizData = shuffleArray(quizData);

  resetLocalStorage();


  createQuestion();

  quizResult.style.display = "none";

  quizContainer.style.display = "block";

};


const displayQuizResult = () => {

  quizResult.style.display = "flex";

  quizContainer.style.display = "none";

  quizResult.innerHTML = "";


  const resultHeading = document.createElement("h2");

  resultHeading.innerHTML = `Nilai Kamu ${score} dari ${MAX_QUESTIONS} soal.`;

  quizResult.appendChild(resultHeading);


  for (let i = 0; i < MAX_QUESTIONS; i++) {

    const resultItem = document.createElement("div");

    resultItem.classList.add("question-container");


    const userAnswer = localStorage.getItem(`userAnswer_${i}`);

    const correctAnswer = quizData[i].correct;


    let answeredCorrectly = userAnswer === correctAnswer;


    if (!answeredCorrectly) {

      resultItem.classList.add("incorrect");

    }


    resultItem.innerHTML = `<div class="question">Question ${i + 1}: ${

      quizData[i].question

    }</div>

    <div class="user-answer">Your answer: ${userAnswer || "Not Answered"}</div>

    <div class="correct-answer">Correct answer: ${correctAnswer}</div>`;


    quizResult.appendChild(resultItem);

  }

  

  const retakeBtn = document.createElement("button");

  retakeBtn.classList.add("retake-btn");

  retakeBtn.innerHTML = "Mengulang Quiz";

  retakeBtn.addEventListener("click", retakeQuiz);

  quizResult.appendChild(retakeBtn);

  


};


const displayNextQuestion = () => {

  if (questionNumber >= MAX_QUESTIONS - 1) {

    displayQuizResult();

    return;

  }


  questionNumber++;

  createQuestion();

};


nextBtn.addEventListener("click", displayNextQuestion);


startBtn.addEventListener("click", () => {

  startBtnContainer.style.display = "none";

  quizContainer.style.display = "block";

  createQuestion();

});        

    

  </script>


</div>