// This file manages the quiz functionality, including loading questions from the JSON file, displaying them to the user, and collecting answers. document.addEventListener('DOMContentLoaded', function () { const quizContainer = document.getElementById('quiz'); const resultsContainer = document.getElementById('results'); const timerDisplay = document.getElementById('time'); const timerLabel = document.getElementById('timer-label'); // Add a label for the timer const version = localStorage.getItem('quizVersion'); const quizId = 'quiz'; let questions = []; let currentQuestionIndex = 0; let score = 0; let quizResults = []; const initialTimeLeft = 25000; // 25 seconds in milliseconds let timeLeft = initialTimeLeft; let timer; let startTime = new Date().toISOString(); // Set timer label based on version if (version === 'ukrainian') { timerLabel.textContent = 'Час, що залишився:'; } else { timerLabel.textContent = 'Time remaining:'; } // Generate and store browser ID function generateBrowserId() { return 'xxxx-xxxx-4xxx-yxxx-xxxx-xxxx-xxxx-xxxx'.replace(/[xy]/g, function(c) { const r = Math.random() * 16 | 0, v = c === 'x' ? r : (r & 0x3 | 0x8); return v.toString(16); }); } const browserId = localStorage.getItem('browserId') || generateBrowserId(); localStorage.setItem('browserId', browserId); const browserInfo = { userAgent: navigator.userAgent, screenResolution: `${window.screen.width}x${window.screen.height}`, operatingSystem: navigator.platform, hostname: window.location.hostname // Store hostname }; // Check if there is a completed quiz const allQuizzes = JSON.parse(localStorage.getItem('allQuizzes') || '{}'); if (allQuizzes[quizId] && allQuizzes[quizId].completed) { alert("Ви вже пройшли дослідження. Дякуємо! Перенаправлення на головну сторінку тесту."); window.location.href = 'index.html'; return; } fetch('data/questions.json') .then(response => response.json()) .then(data => { if (!data.quizzes || !data.quizzes[version] || !data.quizzes[version].questions) { throw new Error('Invalid quiz data or version'); } questions = data.quizzes[version].questions; questions = shuffleArray(questions); // Randomize the order of questions displayQuestion(); }) .catch(error => { console.error('Error loading quiz:', error); quizContainer.innerHTML = '
Error loading quiz. Please try again later.
'; }); // Function to shuffle an array function shuffleArray(array) { for (let i = array.length - 1; i > 0; i--) { const j = Math.floor(Math.random() * (i + 1)); [array[i], array[j]] = [array[j], array[i]]; } return array; } function displayQuestion() { if (!questions || currentQuestionIndex >= questions.length) { showResults(); return; } // Complete timer reset stopTimer(); timeLeft = initialTimeLeft; // Reset to initial time timerDisplay.textContent = Math.ceil(timeLeft / 1000); // Display in whole seconds const question = questions[currentQuestionIndex]; const imageVersion = version === 'translated' ? 'original' : version; const imagePath = `/images/${question.chart}-${imageVersion}.${question.image}`; document.getElementById('chart').innerHTML = `