// Variables globales let currentLevel = 1; let timeLeft = 120; let score = 0; let correctAnswer; let questionList = []; var levelSelect = document.getElementById("level-select"); var infoDiv = document.getElementById("info"); var scoreDiv = document.getElementById("score"); var clock = document.getElementById("clock"); clock.style.display = "none"; var r = document.getElementById("r"); r.style.display = "none"; // Para nivel 10 let summands = [9, 6, 3, 5]; // Secuencia de sumandos para el nivel 10 let summandIndex = 0; // Índice del sumando actual let summandCount = 0; // Cuenta cuántas preguntas se han generado con el sumando actual // Inicia el juego con el nivel seleccionado function startGame(level) { infoDiv.innerHTML = "Level "+level; currentLevel = level; timeLeft = 120; score = 0; // Muestra las opciones document.getElementById('options').style.display = "block"; // Oculta el resultado document.getElementById('results').style.display = "none"; generateQuestion(); startTimer(); levelSelect.style.display = "none"; clock.style.display = "block"; r.style.display = "block"; } // Genera una pregunta basada en el nivel actual function generateQuestion() { let num1, num2, operator, questionText, wrongOptions = []; switch (currentLevel) { case 1: operator = '+'; num1 = Math.floor(Math.random() * 5) + 1; // Genera un número aleatorio entre 1 y 5 num2 = Math.floor(Math.random() * (6 - num1)); // Genera un número aleatorio entre 0 y (6 - num1) correctAnswer = num1 + num2; questionText = `${num1} ${operator} ${num2} = ?`; // Añadir la pregunta a la lista questionList.push(questionText); // Generamos opciones incorrectas for (let i = 0; i < 2; i++) { let incorrectAnswer = correctAnswer + (Math.random() > 0.5 ? 1 : -1) * (i + 1); // Asegurándose de que la opción incorrecta no sea negativa y que sea <= 10 if (incorrectAnswer >= 0 && incorrectAnswer <= 10) wrongOptions.push(incorrectAnswer); } break; case 2: operator = '-'; num1 = Math.floor(Math.random() * 4) + 1; // Genera un número aleatorio entre 1 y 4 num2 = Math.floor(Math.random() * (num1 + 1)); // Genera un número aleatorio entre 0 y num1 (inclusive) correctAnswer = num1 - num2; questionText = `${num1} ${operator} ${num2} = ?`; questionList.push(questionText); // Generamos opciones incorrectas for (let i = 0; i < 2; i++) { let incorrectAnswer = correctAnswer + (Math.random() > 0.5 ? 1 : -1) * (i + 1); // Asegurándose de que la opción incorrecta no sea negativa y que sea <= 10 if (incorrectAnswer >= 0 && incorrectAnswer <= 10) wrongOptions.push(incorrectAnswer); } break; case 3: operator = '+'; num1 = Math.floor(Math.random() * 4) + 1; // Genera un número aleatorio entre 1 y 5 num2 = Math.floor(Math.random() * 5) + 1; // Genera un número aleatorio entre 1 y 5 correctAnswer = num1 + num2; questionText = `${num1} ${operator} ${num2} = ?`; questionList.push(questionText); // Generamos opciones incorrectas for (let i = 0; i < 2; i++) { let incorrectAnswer = correctAnswer + (Math.random() > 0.5 ? 1 : -1) * (i + 1); // Asegurándose de que la opción incorrecta no sea negativa y que sea <= 10 if (incorrectAnswer >= 0 && incorrectAnswer <= 10) wrongOptions.push(incorrectAnswer); } break; case 4: operator = '-'; num1 = Math.floor(Math.random() * 6) + 1; // Genera un número aleatorio entre 1 y 6 num2 = Math.floor(Math.random() * (num1 + 1)); // Genera un número aleatorio entre 0 y num1 (inclusive) correctAnswer = num1 - num2; questionText = `${num1} ${operator} ${num2} = ?`; questionList.push(questionText); // Generamos opciones incorrectas for (let i = 0; i < 2; i++) { let incorrectAnswer = correctAnswer + (Math.random() > 0.5 ? 1 : -1) * (i + 1); // Asegurándose de que la opción incorrecta no sea negativa y que sea <= 10 if (incorrectAnswer >= 0 && incorrectAnswer <= 10) wrongOptions.push(incorrectAnswer); } break; case 5: operator = '+'; num1 = Math.floor(Math.random() * 4) + 1; // Genera un número aleatorio entre 1 y 5 num2 = Math.floor(Math.random() * 9) + 1; // Genera un número aleatorio entre 0 y (6 - num1) correctAnswer = num1 + num2; questionText = `${num1} ${operator} ${num2} = ?`; questionList.push(questionText); // Generamos opciones incorrectas for (let i = 0; i < 2; i++) { let incorrectAnswer = correctAnswer + (Math.random() > 0.5 ? 1 : -1) * (i + 1); // Asegurándose de que la opción incorrecta no sea negativa y que sea <= 10 if (incorrectAnswer >= 0 && incorrectAnswer <= 10) wrongOptions.push(incorrectAnswer); } break; case 6: operator = '-'; num1 = Math.floor(Math.random() * 10) + 1; // Genera un número aleatorio entre 1 y 10 num2 = Math.floor(Math.random() * (num1 + 1)); // Genera un número aleatorio entre 0 y num1 (inclusive) correctAnswer = num1 - num2; questionText = `${num1} ${operator} ${num2} = ?`; questionList.push(questionText); // Generamos opciones incorrectas for (let i = 0; i < 2; i++) { let incorrectAnswer = correctAnswer + (Math.random() > 0.5 ? 1 : -1) * (i + 1); // Asegurándose de que la opción incorrecta no sea negativa y que sea <= 10 if (incorrectAnswer >= 0 && incorrectAnswer <= 10) wrongOptions.push(incorrectAnswer); } break; case 7: operator = '+'; let validNum1 = [4, 5, 8, 9]; // números válidos para num1 num1 = validNum1[Math.floor(Math.random() * validNum1.length)]; // Selecciona un número al azar de los números válidos num2 = Math.floor(Math.random() * 8) + 2; // Genera un número aleatorio entre 2 y 9 correctAnswer = num1 + num2; questionText = `${num1} ${operator} ${num2} = ?`; questionList.push(questionText); // Generamos opciones incorrectas for (let i = 0; i < 2; i++) { let incorrectAnswer = correctAnswer + (Math.random() > 0.5 ? 1 : -1) * (i + 1); // Asegurándose de que la opción incorrecta no sea negativa y que sea <= 20 if (incorrectAnswer >= 0 && incorrectAnswer <= 20) wrongOptions.push(incorrectAnswer); } break; case 8: operator = '-'; num1 = Math.floor(Math.random() * 6) + 5; // Genera un número aleatorio entre 5 y 10 num2 = Math.floor(Math.random() * (num1 - 2)) + 2; // Genera un número aleatorio entre 2 y num1 correctAnswer = num1 - num2; questionText = `${num1} ${operator} ${num2} = ?`; questionList.push(questionText); // Generamos opciones incorrectas for (let i = 0; i < 2; i++) { let incorrectAnswer = correctAnswer + (Math.random() > 0.5 ? 1 : -1) * (i + 1); // Asegurándose de que la opción incorrecta no sea negativa y que sea <= 10 if (incorrectAnswer >= 0 && incorrectAnswer <= 10) wrongOptions.push(incorrectAnswer); } break; case 9: operator = 'x'; if (Math.random() > 0.5) { num1 = Math.floor(Math.random() * 5) + 1; // Genera un número aleatorio entre 1 y 5 num2 = Math.floor(Math.random() * 9) + 1; // Genera un número aleatorio entre 1 y 9 } else { num1 = Math.floor(Math.random() * 9) + 1; // Genera un número aleatorio entre 1 y 9 num2 = Math.floor(Math.random() * 5) + 1; // Genera un número aleatorio entre 1 y 5 } correctAnswer = num1 * num2; questionText = `${num1} ${operator} ${num2} = ?`; questionList.push(questionText); // Generamos opciones incorrectas for (let i = 0; i < 2; i++) { let incorrectAnswer = correctAnswer + (Math.random() > 0.5 ? 1 : -1) * (i + 1); // Asegurándose de que la opción incorrecta no sea negativa y que sea <= 45 (el máximo resultado de la multiplicación 9*5) if (incorrectAnswer >= 0 && incorrectAnswer <= 45) wrongOptions.push(incorrectAnswer); } break; case 10: operator = '+'; num1 = summands[summandIndex]; // Toma el sumando actual num2 = Math.floor(Math.random() * 8) + 2; // Genera un número aleatorio entre 2 y 9 correctAnswer = num1 + num2; questionText = `${num1} ${operator} ${num2} = ?`; questionList.push(questionText); // Generamos opciones incorrectas for (let i = 0; i < 2; i++) { let incorrectAnswer = correctAnswer + (Math.random() > 0.5 ? 1 : -1) * (i + 1); // Asegurándose de que la opción incorrecta no sea negativa y que sea <= 18 (el máximo resultado de la suma 9+9) if (incorrectAnswer >= 0 && incorrectAnswer <= 18) wrongOptions.push(incorrectAnswer); } summandCount++; // Si se han generado 15 preguntas con el sumando actual, pasa al siguiente sumando if (summandCount === 15) { summandIndex++; summandCount = 0; } // Si el índice de sumandos ha alcanzado la longitud de sumandos, reiniciarlo a 0 if (summandIndex === summands.length) { summandIndex = 0; } break; case 11: operator = '-'; num1 = Math.floor(Math.random() * 11) + 7; // Genera un número aleatorio entre 7 y 17 num2 = Math.floor(Math.random() * (num1 - 2)) + 2; // Genera un número aleatorio entre 2 y (num1 - 2) correctAnswer = num1 - num2; questionText = `${num1} ${operator} ${num2} = ?`; questionList.push(questionText); // Generamos opciones incorrectas for (let i = 0; i < 2; i++) { let incorrectAnswer = correctAnswer + (Math.random() > 0.5 ? 1 : -1) * (i + 1); // Asegurándose de que la opción incorrecta no sea negativa y que sea <= num1 if (incorrectAnswer >= 0 && incorrectAnswer <= num1) wrongOptions.push(incorrectAnswer); } break; case 12: operator = 'x'; if (Math.random() > 0.5) { num1 = Math.floor(Math.random() * 9) + 1; // Genera un número aleatorio entre 1 y 5 num2 = Math.floor(Math.random() * 9) + 1; // Genera un número aleatorio entre 1 y 9 } else { num1 = Math.floor(Math.random() * 9) + 1; // Genera un número aleatorio entre 1 y 9 num2 = Math.floor(Math.random() * 9) + 1; // Genera un número aleatorio entre 1 y 5 } correctAnswer = num1 * num2; questionText = `${num1} ${operator} ${num2} = ?`; questionList.push(questionText); // Generamos opciones incorrectas for (let i = 0; i < 2; i++) { let incorrectAnswer = correctAnswer + (Math.random() > 0.5 ? 1 : -1) * (i + 1); // Asegurándose de que la opción incorrecta no sea negativa y que sea <= 45 (el máximo resultado de la multiplicación 9*5) if (incorrectAnswer >= 0 && incorrectAnswer <= 45) wrongOptions.push(incorrectAnswer); } break; case 13: operator = '÷'; num1 = Math.floor(Math.random() * 9) + 1; // Genera un número aleatorio entre 1 y 9 num2 = Math.floor(Math.random() * 9) + 1; // Genera un número aleatorio entre 1 y 9 let product = num1 * num2; // El producto siempre será el dividendo let dividend = product; // Aleatoriamente seleccionamos uno de los factores para ser el divisor let divisor = Math.random() > 0.5 ? num1 : num2; // Y la respuesta correcta será el factor restante correctAnswer = dividend / divisor; questionText = `${dividend} ${operator} ${divisor} = ?`; // Generamos opciones incorrectas for (let i = 0; i < 2; i++) { let incorrectAnswer = correctAnswer + (Math.random() > 0.5 ? 1 : -1) * (i + 1); // Asegurándose de que la opción incorrecta es un número entero y que no sea igual al divisor if (Number.isInteger(incorrectAnswer) && incorrectAnswer !== divisor) wrongOptions.push(incorrectAnswer); } break; case 14: operator = '+'; if (score < 15) { num1 = 7; num2 = Math.floor(Math.random() * 7) + 3; // Genera un número aleatorio entre 3 y 9 } else if (score < 30) { num1 = 9; num2 = Math.floor(Math.random() * 8) + 2; // Genera un número aleatorio entre 2 y 9 } else if (score < 45) { num1 = 6; num2 = Math.floor(Math.random() * 8) + 2; // Genera un número aleatorio entre 2 y 9 } else { num1 = 8; num2 = Math.floor(Math.random() * 8) + 2; // Genera un número aleatorio entre 2 y 9 } correctAnswer = num1 + num2; questionText = `${num1} ${operator} ${num2} = ?`; questionList.push(questionText); // Generamos opciones incorrectas for (let i = 0; i < 2; i++) { let incorrectAnswer = correctAnswer + (Math.random() > 0.5 ? 1 : -1) * (i + 1); // Asegurándose de que la opción incorrecta no sea negativa y que sea <= 20 if (incorrectAnswer >= 0 && incorrectAnswer <= 20) wrongOptions.push(incorrectAnswer); } break; case 15: operator = '-'; num1 = Math.floor(Math.random() * 16) + 4; // Genera un número aleatorio entre 4 y 19 num2 = Math.floor(Math.random() * 8) + 2; // Genera un número aleatorio entre 2 y 9 // Asegurarse de que el resultado no sea negativo while (num1 - num2 < 0) { num1 = Math.floor(Math.random() * 16) + 4; num2 = Math.floor(Math.random() * 8) + 2; } correctAnswer = num1 - num2; questionText = `${num1} ${operator} ${num2} = ?`; questionList.push(questionText); // Generamos opciones incorrectas for (let i = 0; i < 2; i++) { let incorrectAnswer = correctAnswer + (Math.random() > 0.5 ? 1 : -1) * (i + 1); // Asegurándose de que la opción incorrecta no sea negativa y que sea <= 20 if (incorrectAnswer >= 0 && incorrectAnswer <= 20) wrongOptions.push(incorrectAnswer); } break; case 16: operator = 'x'; if (Math.random() > 0.5) { num1 = Math.floor(Math.random() * 9) + 1; // Genera un número aleatorio entre 1 y 5 num2 = Math.floor(Math.random() * 9) + 1; // Genera un número aleatorio entre 1 y 9 } else { num1 = Math.floor(Math.random() * 9) + 1; // Genera un número aleatorio entre 1 y 9 num2 = Math.floor(Math.random() * 9) + 1; // Genera un número aleatorio entre 1 y 5 } correctAnswer = num1 * num2; questionText = `${num1} ${operator} ${num2} = ?`; questionList.push(questionText); // Generamos opciones incorrectas for (let i = 0; i < 2; i++) { let incorrectAnswer = correctAnswer + (Math.random() > 0.5 ? 1 : -1) * (i + 1); // Asegurándose de que la opción incorrecta no sea negativa y que sea <= 45 (el máximo resultado de la multiplicación 9*5) if (incorrectAnswer >= 0 && incorrectAnswer <= 45) wrongOptions.push(incorrectAnswer); } break; case 17: operator = '÷'; let factor1 = Math.floor(Math.random() * 8) + 2; // Genera un factor aleatorio entre 2 y 9 let factor2 = Math.floor(Math.random() * 8) + 2; // Genera un factor aleatorio entre 2 y 9 num1 = factor1 * factor2; // El dividendo es el producto de los dos factores num2 = Math.random() > 0.5 ? factor1 : factor2; // El divisor es uno de los factores correctAnswer = num1 / num2; questionText = `${num1} ${operator} ${num2} = ?`; questionList.push(questionText); // Generamos opciones incorrectas for (let i = 0; i < 2; i++) { let incorrectAnswer = correctAnswer + (Math.random() > 0.5 ? 1 : -1) * (i + 1); // Asegurándose de que la opción incorrecta sea un número entero y que sea <= 81 (el producto máximo de dos números entre 2 y 9) if (Number.isInteger(incorrectAnswer) && incorrectAnswer >= 1 && incorrectAnswer <= 81) wrongOptions.push(incorrectAnswer); } break; case 18: operator = '+'; num1 = Math.floor(Math.random() * 9) + 11; // Genera un número aleatorio entre 11 y 19 num2 = Math.floor(Math.random() * 8) + 2; // Genera un número aleatorio entre 2 y 9 correctAnswer = num1 + num2; questionText = `${num1} ${operator} ${num2} = ?`; questionList.push(questionText); // Generamos opciones incorrectas for (let i = 0; i < 2; i++) { let incorrectAnswer = correctAnswer + (Math.random() > 0.5 ? 1 : -1) * (i + 1); // Asegurándose de que la opción incorrecta no sea negativa y que sea <= 28 (la suma máxima de dos números entre 11-19 y 2-9) if (incorrectAnswer >= 0 && incorrectAnswer <= 28) wrongOptions.push(incorrectAnswer); } break; case 19: operator = '-'; num1 = Math.floor(Math.random() * 9) + 11; // Genera un número aleatorio entre 4 y 19 num2 = Math.floor(Math.random() * 8) + 2; // Genera un número aleatorio entre 2 y 9 // Asegurarse de que el resultado no sea negativo while (num1 - num2 < 0) { num1 = Math.floor(Math.random() * 9) + 11; num2 = Math.floor(Math.random() * 8) + 2; } correctAnswer = num1 - num2; questionText = `${num1} ${operator} ${num2} = ?`; questionList.push(questionText); // Generamos opciones incorrectas for (let i = 0; i < 2; i++) { let incorrectAnswer = correctAnswer + (Math.random() > 0.5 ? 1 : -1) * (i + 1); // Asegurándose de que la opción incorrecta no sea negativa y que sea <= 20 if (incorrectAnswer >= 0 && incorrectAnswer <= 20) wrongOptions.push(incorrectAnswer); } break; case 20: operator = 'x'; if (Math.random() > 0.5) { num1 = Math.floor(Math.random() * 9) + 1; // Genera un número aleatorio entre 1 y 5 num2 = Math.floor(Math.random() * 9) + 1; // Genera un número aleatorio entre 1 y 9 } else { num1 = Math.floor(Math.random() * 9) + 1; // Genera un número aleatorio entre 1 y 9 num2 = Math.floor(Math.random() * 9) + 1; // Genera un número aleatorio entre 1 y 5 } correctAnswer = num1 * num2; questionText = `${num1} ${operator} ${num2} = ?`; questionList.push(questionText); // Generamos opciones incorrectas for (let i = 0; i < 2; i++) { let incorrectAnswer = correctAnswer + (Math.random() > 0.5 ? 1 : -1) * (i + 1); // Asegurándose de que la opción incorrecta no sea negativa y que sea <= 45 (el máximo resultado de la multiplicación 9*5) if (incorrectAnswer >= 0 && incorrectAnswer <= 45) wrongOptions.push(incorrectAnswer); } break; case 21: operator = '÷'; let factor1b = Math.floor(Math.random() * 8) + 2; // Genera un factor aleatorio entre 2 y 9 let factor2b = Math.floor(Math.random() * 8) + 2; // Genera un factor aleatorio entre 2 y 9 num1 = factor1b * factor2b; // El dividendo es el producto de los dos factores num2 = Math.random() > 0.5 ? factor1b : factor2b; // El divisor es uno de los factores correctAnswer = num1 / num2; questionText = `${num1} ${operator} ${num2} = ?`; questionList.push(questionText); // Generamos opciones incorrectas for (let i = 0; i < 2; i++) { let incorrectAnswer = correctAnswer + (Math.random() > 0.5 ? 1 : -1) * (i + 1); // Asegurándose de que la opción incorrecta sea un número entero y que sea <= 81 (el producto máximo de dos números entre 2 y 9) if (Number.isInteger(incorrectAnswer) && incorrectAnswer >= 1 && incorrectAnswer <= 81) wrongOptions.push(incorrectAnswer); } break; // Aquí irán los demás niveles } ; // Muestra la pregunta document.getElementById('question').innerText = questionText; // Genera las opciones de respuesta generateOptions(wrongOptions); } function generateOptions(wrongOptions) { let options = []; options.push(correctAnswer); wrongOptions.forEach(option => { options.push(option); }); // Mezcla las opciones options.sort(() => Math.random() - 0.5); // Muestra las opciones let optionsDiv = document.getElementById('options'); optionsDiv.innerHTML = ''; options.forEach(option => { let optionButton = document.createElement('button'); optionButton.innerText = option; // Actualiza la llamada onclick para pasar el botón a handleAnswer() optionButton.onclick = function () { handleAnswer(option, this); }; optionsDiv.appendChild(optionButton); }); } // Llamada cuando el usuario selecciona una respuesta function handleAnswer(answer, button) { if (answer === correctAnswer) { score++; scoreDiv.innerHTML = "puntos "+score; button.className += " correct"; setTimeout(function () { button.classList.remove("correct"); }, 1000); } else { score--; scoreDiv.innerHTML = "puntos "+score; button.className += " incorrect"; setTimeout(function () { button.classList.remove("incorrect"); }, 1000); } if (timeLeft > 0) { generateQuestion(); } } // Inicia la cuenta regresiva function startTimer() { let clock = document.getElementById('clock'); let countdown = setInterval(function () { if (timeLeft <= 0) { clearInterval(countdown); showResults(); } else { let minutes = Math.floor(timeLeft / 60); let seconds = timeLeft % 60; clock.innerText = `${minutes < 10 ? '0' + minutes : minutes}:${seconds < 10 ? '0' + seconds : seconds}`; timeLeft--; } }, 1000); } // Muestra los resultados finales function showResults() { // Muestra el resultado document.getElementById('results').style.display = "block"; if (score < 0) { score = 0 } document.getElementById('results').innerText = `Tu puntuación final es: ${score} `; // Oculta las opciones document.getElementById('options').style.display = "none"; //muestra niveles otra vez levelSelect.style.display = "block"; document.getElementById('copyButton').style.display = 'block'; } function copyQuestions() { let textToCopy = ""; let counter = 0; questionList.slice(0, 60).forEach((question, index) => { // Remueve el signo de interrogación al final de la pregunta let cleanedQuestion = question.replace(" = ?", ""); textToCopy += cleanedQuestion; counter++; if (counter === 4) { // Número de cuentas por fila textToCopy += "\r\n"; // Dos caracteres: retorno de carro y nueva línea counter = 0; } else { textToCopy += "\t"; // Tabulador entre cuentas en la misma fila } }); let tempInput = document.createElement("textarea"); tempInput.value = textToCopy; document.body.appendChild(tempInput); tempInput.select(); document.execCommand("copy"); document.body.removeChild(tempInput); alert('Preguntas copiadas al portapapeles'); } � 2025 olesur.com