v2.5.2
Giriş yap

Sayfa yenilenene kadar iconlarin kaydedilmesini istiyorum

mertbrmdev
179 defa görüntülendi ve 1 kişi tarafından değerlendirildi

Iconlarin finish butonuna tiklandiktan sonra cagrilmasi ve localStorageda kaydedilmesini istiyorum. Iconlar geliyor ama localStorage'a kaydedemiyorum.
Ne onerirsiniz? veya chatgptye nasil sormam gerekiyor?
Codepen linkine buradan ulaşabilirsiniz

// Function to display feedback icons for answers
function someFunctionWhereYouNeedCurrentQuestionData(currentQuestionData) {
  currentQuestionData.answers.forEach((_, index) => {
    const checkbox = document.getElementById(`answer-${index}`);
    const isCorrect = currentQuestionData.correct.includes(index);
    const feedbackIcon = document.createElement("span");
    feedbackIcon.classList.add("feedback-icon");
    feedbackIcon.textContent = isCorrect ? "✅" : "❌";
    checkbox.nextElementSibling.appendChild(feedbackIcon);
    checkbox.nextElementSibling.classList.add(isCorrect ? "correct" : "incorrect");
  });
}

// Function to save currentQuestionData to localStorage
function saveCurrentQuestionData() {
  localStorage.setItem('currentQuestionData', JSON.stringify(currentQuestionData));
}

// Function to load currentQuestionData from localStorage when the page is loaded
window.addEventListener('load', () => {
  const storedData = localStorage.getItem('currentQuestionData');
  if (storedData) {
    currentQuestionData = JSON.parse(storedData);
  }
});

finishButton.addEventListener("click", () => {
  // Calculate the score
  const finalScore = calculateScore();
  
  // Set the currentQuestionData for the current question
  currentQuestionData = questions[currentQuestion];

  // Call the function to display feedback icons for answers
  someFunctionWhereYouNeedCurrentQuestionData(currentQuestionData);
  
  // Display the score and hide buttons
  const resultContainer = document.getElementById("result-container");
  resultContainer.innerHTML = `Your Point: ${finalScore}<br> Pass Point: 85 <br> Total Points: 100`;
  resultContainer.style.display = "block";
  finishButton.style.display = "none";
  checkAnswersButton.style.display = "none"; // Hide the "Check Answers" button

  // Save the currentQuestionData after updating it
  saveCurrentQuestionData();
});
Cevap yaz
Cevaplar (0)
Henüz kimse cevap yazmadı. İlk cevap yazan sen ol!