-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
84 lines (54 loc) · 2.01 KB
/
script.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
document.addEventListener('DOMContentLoaded', () =>{
let currentQuestionIndex = 0;
let pontos = 0;
const questions = document.querySelectorAll('.question');
const nextButton = document.getElementById('nextButton');
const resultElement = document.getElementById('result');
const timerDisplay = document.getElementById('time');
let timer;
let timeLeft = 60;
function showQuestion(index){
const currentQuestion = questions[index];
currentQuestion.classList.add('active');
resetTimer();
}
function goToNextQuestion(){
const curretQuestion = questions[currentQuestionIndex];
const selectedAnswer = curretQuestion.querySelector('input[type="radio"]:checked');
if(selectedAnswer){
if(selectedAnswer.value === curretQuestion.getAttribute('data-correct')){
pontos++;
}
curretQuestion.classList.remove('active');
currentQuestionIndex++;
if(currentQuestionIndex < questions.length){
showQuestion(currentQuestionIndex);
}else {
nextButton.style.display = 'none';
resultElement.style.display = 'block';
resultElement.textContent = `Você marcou ${pontos} pontos`;
clearInterval(timer);
timerDisplay.textContent = 'Encerrado!'
}
}
}
function resetTimer(){
clearInterval(timer);
timeLeft = 60;
updateTimerDisplay();
timer = setInterval(() =>{
timeLeft--;
updateTimerDisplay();
if(timeLeft === 0) {
clearInterval(timer);
nextButton.style.display = 'none';
timerDisplay.textContent = `Tempo finalizado`;
}
}, 1000);
}
function updateTimerDisplay(){
timerDisplay.textContent = `Tempo restante ${timeLeft}s`;
}
nextButton.addEventListener('click', goToNextQuestion);
showQuestion(currentQuestionIndex);
});