-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
187 lines (157 loc) · 6.79 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
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
// Axel Cotón Gutiérrez Copyright 2023
// Variables globales para el número máximo de ceros y representación de potencias de 10
let maxZeros = 3;
let naturalRepresentation = true; // Variable para representación de potencias de 10 como números naturales
let currentQuestion = null;
// Función para generar una pregunta de descomposición polinómica
function generatePolynomialQuestion() {
// Genera un número aleatorio entre 1 y 10^maxZeros
const maxNumber = Math.pow(10, maxZeros +1) - 1;
const number = Math.floor(Math.random() * maxNumber) + 1;
// Construye la pregunta completa
const question = `Descompón el número ${formatNumberWithSpaces(number)} en su forma polinómica:`;
// Crea un objeto pregunta y respuesta
const answer = number.toString().split("").map(Number);
return { question, answer };
}
// Función para dar formato al número con espacios de mil
function formatNumberWithSpaces(number) {
// Convierte el número a una cadena
const numberStr = number.toString();
// Si el número tiene cuatro cifras, no separamos con espacios
if (numberStr.length === 4) {
return numberStr;
}
// Divide la cadena en grupos de tres cifras desde el final
const groups = [];
let i = numberStr.length;
while (i > 0) {
groups.push(numberStr.slice(Math.max(0, i - 3), i));
i -= 3;
}
// Une los grupos con espacios y devuelve el resultado
return groups.reverse().join(' ');
}
// Función para mostrar una nueva pregunta
function displayQuestion() {
currentQuestion = generatePolynomialQuestion();
questionText.innerHTML = currentQuestion.question;
result.textContent = "";
checkAnswerButton.disabled = false;
nextQuestionButton.style.display = "none";
// Crear inputs para las cifras significativas
userAnswerContainer.innerHTML = "";
for (let i = 0; i < currentQuestion.answer.length; i++) {
const input = document.createElement("input");
input.type = "number";
input.min = 0;
input.required = true;
input.classList.add("input-digit");
userAnswerContainer.appendChild(input);
if (i < currentQuestion.answer.length - 1) {
if (!naturalRepresentation) {
userAnswerContainer.appendChild(document.createTextNode(" × 10"));
const exponent = currentQuestion.answer.length - 1 - i;
if (exponent !== 1) {
userAnswerContainer.appendChild(document.createElement("sup")).textContent = exponent;
}
userAnswerContainer.appendChild(document.createTextNode(" + "));
} else {
const exponent = currentQuestion.answer.length - 1 - i; // Calcula el exponente
let exponentStr;
if (exponent === 0) {
exponentStr = "";
} else if (exponent === 1) {
exponentStr = "10";
} else {
exponentStr = formatNumberWithSpaces(Math.pow(10, exponent)); // Formatea el exponente con espacios de mil
}
userAnswerContainer.appendChild(document.createTextNode(` × ${exponentStr}`));
userAnswerContainer.appendChild(document.createTextNode(" + "));
}
}
}
}
// Función para mostrar la siguiente pregunta
function nextQuestion() {
displayQuestion();
}
// Función para comprobar la respuesta del usuario
function checkAnswer() {
const userResponse = Array.from(userAnswerContainer.querySelectorAll("input")).map(input => parseInt(input.value));
const correctAnswer = currentQuestion.answer;
const formattedCorrectAnswer = formatCorrectAnswer(correctAnswer, naturalRepresentation);
if (arraysAreEqual(userResponse, correctAnswer)) {
result.textContent = "¡Respuesta Correcta!";
result.style.color = "green";
} else {
result.innerHTML = "Respuesta Incorrecta. La respuesta correcta es " + formattedCorrectAnswer;
result.style.color = "red";
}
checkAnswerButton.disabled = true;
nextQuestionButton.style.display = "block";
}
// Función para formatear la respuesta correcta como se muestra en la pregunta
function formatCorrectAnswer(correctAnswer, naturalRepresentation) {
const formattedAnswer = [];
for (let i = 0; i < correctAnswer.length; i++) {
const digit = correctAnswer[i];
if (!naturalRepresentation) {
formattedAnswer.push(`${digit}`);
const exponent = currentQuestion.answer.length - 1 - i;
if (exponent > 1) {
formattedAnswer.push(` × 10<sup>${exponent}</sup>`);
} else if (exponent === 1) {
formattedAnswer.push(" × 10");
}
} else {
const multiplier = Math.pow(10, currentQuestion.answer.length - 1 - i);
const formattedMultiplier = formatNumberWithSpaces(multiplier);
formattedAnswer.push(`${digit} × ${formattedMultiplier}`);
}
if (i < correctAnswer.length - 1) {
formattedAnswer.push(" + ");
}
}
return formattedAnswer.join("");
}
// Escucha el evento de cambio en los radios de selección de ceros
const radioButtons = document.getElementsByName("maxZeros");
for (const radioButton of radioButtons) {
radioButton.addEventListener("change", function () {
maxZeros = parseInt(this.value);
displayQuestion();
checkAnswerButton.disabled = false; // Habilitar el botón "Comprobar" después de cambiar las unidades
});
}
// Escucha el evento de cambio en la representación de potencias de 10
const powerRepresentationRadioButtons = document.getElementsByName("powerRepresentation");
for (const radioButton of powerRepresentationRadioButtons) {
radioButton.addEventListener("change", function () {
naturalRepresentation = this.value === "natural";
displayQuestion();
});
}
// Elementos HTML
const questionText = document.getElementById("question-text");
const checkAnswerButton = document.getElementById("check-answer");
const result = document.getElementById("result");
const nextQuestionButton = document.getElementById("next-question");
const userAnswerContainer = document.getElementById("user-answer-container");
// Mostrar la primera pregunta
displayQuestion();
// Eventos de los botones
checkAnswerButton.addEventListener("click", checkAnswer);
nextQuestionButton.addEventListener("click", nextQuestion);
// Función para comparar dos arreglos
function arraysAreEqual(arr1, arr2) {
if (arr1.length !== arr2.length) {
return false;
}
for (let i = 0; i < arr1.length; i++) {
if (arr1[i] !== arr2[i]) {
return false;
}
}
return true;
}