-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjavascript.js
More file actions
213 lines (178 loc) · 9.6 KB
/
javascript.js
File metadata and controls
213 lines (178 loc) · 9.6 KB
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
const famosos = {
"Patriotas_Argentinos": [
{nombre: "José de San Martín", descripcion: "Libertador de Argentina, Chile y Perú", imagen: "./img/proceres/sanmartin.webp"},
{nombre: "Manuel Belgrano", descripcion: "Creador de la bandera argentina", imagen: "./img/proceres/manuel belgrano.webp"},
{nombre: "Juan Martín de Pueyrredón", descripcion: "Director Supremo que apoyó las campañas libertadoras", imagen: "./img/proceres/pueyrredon.webp"},
{nombre: "Martín Miguel de Güemes", descripcion: "Defensor del norte argentino con sus gauchos", imagen: "./img/proceres/martin guemes.webp"},
{nombre: "Juana Azurduy", descripcion: "Heroína de la independencia, luchó junto a las tropas patriotas", imagen: "./img/proceres/jauana azurduy.webp"}
],
"Presidentes_Argentinos": [
{nombre: "Domingo Faustino Sarmiento", descripcion: "Impulsor de la educación pública", imagen: "./img/presidentes/faustino sarmiento.webp"},
{nombre: "Bartolomé Mitre", descripcion: "Primer presidente de la Nación unificada", imagen: "./img/presidentes/bartolome mitre.webp"},
{nombre: "Hipólito Yrigoyen", descripcion: "Primer presidente elegido por voto secreto y obligatorio", imagen: "./img/presidentes/hipolito yrigoyen.webp"},
{nombre: "Juan Domingo Perón", descripcion: "Figura central del peronismo, tres veces presidente", imagen: "./img/presidentes/peron.webp"},
{nombre: "Raúl Alfonsín", descripcion: "Presidente del retorno democrático en 1983", imagen: "./img/presidentes/raul alfonsin.webp"}
],
"Actores_Argentinos": [
{nombre: "Ricardo Darín", descripcion: "Actor de cine reconocido internacionalmente", imagen: "./img/actores/ricardo darin.webp"},
{nombre: "Norma Aleandro", descripcion: "Actriz premiada en Hollywood y referente del teatro", imagen: "./img/actores/norma leandro.webp"},
{nombre: "Luis Brandoni", descripcion: "Actor de cine, teatro y televisión", imagen: "./img/actores/luis brandoni.webp"},
{nombre: "Mercedes Morán", descripcion: "Actriz destacada en cine y TV", imagen: "./img/actores/mercedes moran.webp"},
{nombre: "Guillermo Francella", descripcion: "Actor de comedia y drama, muy popular en Argentina", imagen: "./img/actores/guillermo francella.webp"}
]
};
const categoryNames = {
"Patriotas_Argentinos": "Patriota",
"Presidentes_Argentinos": "Presidente",
"Actores_Argentinos": "Actor"
};
let currentQuestionIndex = 0;
let score = 0;
let questions = [];
let timer;
let timeLeft = 10;
let usedPersons = [];
function generateQuestions() {
questions = [];
usedPersons = [];
const allPersons = [];
for (let category in famosos) {
famosos[category].forEach(person => {
allPersons.push({...person, category});
});
}
// Mezclar todas las personas
const shuffledPersons = allPersons.sort(() => 0.5 - Math.random());
for (let i = 0; i < 10 && i < shuffledPersons.length; i++) {
const correctPerson = shuffledPersons[i];
usedPersons.push(correctPerson.nombre);
// Obtener opciones incorrectas (que no sean la correcta y no hayan sido usadas)
const wrongOptions = allPersons.filter(p =>
p.nombre !== correctPerson.nombre &&
!usedPersons.includes(p.nombre)
);
// Seleccionar 3 opciones incorrectas aleatorias
const shuffled = wrongOptions.sort(() => 0.5 - Math.random()).slice(0, 3);
// Mezclar la respuesta correcta con las incorrectas
const options = [...shuffled, correctPerson].sort(() => 0.5 - Math.random());
questions.push({
question: correctPerson.descripcion,
correct: correctPerson.nombre,
options: options.map(p => p.nombre),
category: correctPerson.category,
imagen: correctPerson.imagen
});
}
}
function startQuiz() {
currentQuestionIndex = 0;
score = 0;
generateQuestions();
document.getElementById('startScreen').classList.add('hidden');
document.getElementById('resultScreen').classList.add('hidden');
document.getElementById('gameScreen').classList.remove('hidden');
showQuestion();
}
function showQuestion() {
if (currentQuestionIndex >= 10) {
showResults();
return;
}
const question = questions[currentQuestionIndex];
timeLeft = 10;
document.getElementById('currentQuestion').textContent = currentQuestionIndex + 1;
document.getElementById('scoreValue').textContent = score;
document.getElementById('questionText').textContent = question.question;
document.getElementById('categoryBadge').textContent = categoryNames[question.category];
document.getElementById('questionImage').src = question.imagen;
document.getElementById('progressBar').style.width = ((currentQuestionIndex + 1) / 10 * 100) + '%';
const optionsContainer = document.getElementById('optionsContainer');
optionsContainer.innerHTML = '';
const letters = ['A', 'B', 'C', 'D'];
question.options.forEach((option, index) => {
const optionDiv = document.createElement('div');
optionDiv.className = 'option';
optionDiv.innerHTML = `
<div class="option-letter">${letters[index]}</div>
<div>${option}</div>
`;
optionDiv.onclick = () => selectAnswer(option, optionDiv);
optionsContainer.appendChild(optionDiv);
});
startTimer();
}
function startTimer() {
clearInterval(timer);
document.getElementById('timer').textContent = `⏱️ ${timeLeft}s`;
timer = setInterval(() => {
timeLeft--;
document.getElementById('timer').textContent = `⏱️ ${timeLeft}s`;
if (timeLeft <= 0) {
clearInterval(timer);
handleTimeout();
}
}, 1000);
}
function selectAnswer(selected, optionDiv) {
clearInterval(timer);
const question = questions[currentQuestionIndex];
const allOptions = document.querySelectorAll('.option');
allOptions.forEach(opt => {
opt.classList.add('disabled');
if (opt.textContent.includes(question.correct)) {
opt.classList.add('correct');
}
});
if (selected === question.correct) {
score += 10;
optionDiv.classList.add('correct');
} else {
optionDiv.classList.add('incorrect');
}
document.getElementById('scoreValue').textContent = score;
setTimeout(() => {
currentQuestionIndex++;
showQuestion();
}, 2000);
}
function handleTimeout() {
const question = questions[currentQuestionIndex];
const allOptions = document.querySelectorAll('.option');
allOptions.forEach(opt => {
opt.classList.add('disabled');
if (opt.textContent.includes(question.correct)) {
opt.classList.add('correct');
}
});
setTimeout(() => {
currentQuestionIndex++;
showQuestion();
}, 2000);
}
function showResults() {
clearInterval(timer);
document.getElementById('gameScreen').classList.add('hidden');
document.getElementById('resultScreen').classList.remove('hidden');
const percentage = (score / 100) * 100;
let message, description;
if (percentage === 100) {
message = "¡Perfecto!";
description = "Eres un maestro de la historia latinoamericana";
} else if (percentage >= 70) {
message = "¡Excelente!";
description = "Muy buen conocimiento";
} else if (percentage >= 40) {
message = "¡Bien hecho!";
description = "Conoces lo básico, pero puedes mejorar";
} else {
message = "Sigue practicando";
description = "Necesitas repasar más historia";
}
document.getElementById('resultMessage').textContent = message;
document.getElementById('finalScore').textContent = score;
document.getElementById('resultDescription').textContent = description;
}
function goToStart() {
document.getElementById('resultScreen').classList.add('hidden');
document.getElementById('startScreen').classList.remove('hidden');
}