Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

100
Vistas
La página muestra la fórmula en lugar del resultado (pero solo para UNO de los casos)

Soy un estudiante que actualmente está aprendiendo JavaScript. Como práctica, quería hacer un lindo aleatorizador de lectura para un amigo con un formulario simple y un proceso de validación de entrada if else. Mis dos primeros casos funcionan como los espero, pero el tercero, el que realmente hace el cálculo, no envía el resultado del cálculo para que se muestre, sino la fórmula. No estoy seguro de dónde me equivoqué.

 function pickfic() { // Get the value of the input fields let minNumChosen = document.getElementById('minNum').value; let maxNumChosen = document.getElementById('maxNum').value; // If input Not a Number or min bigger than max let reply; if (isNaN(minNumChosen) || isNaN(maxNumChosen) || minNumChosen > maxNumChosen ) { reply = "I think you pissed off my sandwich. Also, those numbers make no sense to me."; } // If min is zero else if (minNumChosen == 0) { reply = "Really, dude? You have an Excel line for 'zero'?? Witch."; } else { // if range is correct, randomize number const generateRandomNumber = (minNumChosen, maxNumChosen) => { return Math.floor(Math.random() * (max - min) + min); }; reply = "Today, you should read fic number " + generateRandomNumber + "!"; } document.getElementById("result").innerHTML = reply; }

Para el último caso, la página muestra: "Hoy, debería leer el número fic (minNumChosen, maxNumChosen) => { return Math.floor (Math.random() * (max - min) + min); }!"

Puedes encontrar el codepen aquí .

EDITAR: Resulta que encontré otro error, que probablemente esté basado en la lógica. Parece que para mi función, 2 es mayor que 10. Así que debe juzgarse por el primer dígito...

about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

function pickfic() { // Get the value of the input fields let minNumChosen = document.getElementById('minNum').value; let maxNumChosen = document.getElementById('maxNum').value; // If input Not a Number or min bigger than max let reply; if (isNaN(minNumChosen) || isNaN(maxNumChosen) || minNumChosen > maxNumChosen ) { reply = "I think you pissed off my sandwich. Also, those numbers make no sense to me."; } // If min is zero else if (minNumChosen == 0) { reply = "Really, dude? You have an Excel line for 'zero'?? Witch."; } else { // if range is correct, randomize number const generateRandomNumber = Math.floor(Math.random() * (maxNumChosen - minNumChosen) + minNumChosen); reply = "Today, you should read fic number " + generateRandomNumber + "!"; } document.getElementById("result").innerHTML = reply;

O

Creó una función que toma valores pero no proporcionó el valor mínimo y máximo al llamar a la función generateRandomNumber

 function pickfic() { // Get the value of the input fields let minNumChosen = document.getElementById('minNum').value; let maxNumChosen = document.getElementById('maxNum').value; // If input Not a Number or min bigger than max let reply; if (isNaN(minNumChosen) || isNaN(maxNumChosen) || minNumChosen > maxNumChosen ) { reply = "I think you pissed off my sandwich. Also, those numbers make no sense to me."; } // If min is zero else if (minNumChosen == 0) { reply = "Really, dude? You have an Excel line for 'zero'?? Witch."; } else { // if range is correct, randomize number const generateRandomNumber = (min,max) => { return Math.floor(Math.random() * (max - min) + min); }; reply = "Today, you should read fic number " + generateRandomNumber(minNumChosen, maxNumChosen) + "!"; } document.getElementById("result").innerHTML = reply; }
about 4 years ago · Juan Pablo Isaza Denunciar

0

 function pickfic() { // Get the value of the input fields let minNumChosen = document.getElementById('minNum').value; let maxNumChosen = document.getElementById('maxNum').value; // If input Not a Number or min bigger than max let reply; if (isNaN(minNumChosen) || isNaN(maxNumChosen) || minNumChosen > maxNumChosen ) { reply = "I think you pissed off my sandwich. Also, those numbers make no sense to me."; } // If min is zero else if (minNumChosen == 0) { reply = "Really, dude? You have an Excel line for 'zero'?? Witch."; } else { // if range is correct, randomize number const generateRandomNumber = (min, max) => { return Math.floor(Math.random() * (max - min) + min); }; reply = "Today, you should read fic number " + generateRandomNumber(parseInt(minNumChosen), parseInt(maxNumChosen)) + "!"; } document.getElementById("result").innerHTML = reply; }
 <input id="minNum" placeholder="min"> <input id="maxNum" placeholder="max"> <div id="result"></div> <button onclick=pickfic()>Click</button>

Tuviste que agregar paréntesis para generarRandomNumber()

Y también convierta minNumChosen y maxNumChosen en enteros con parseInt().

También hubo otro error en el que no nombró los parámetros de su función generateRandomNumber (min, max).

about 4 years ago · Juan Pablo Isaza Denunciar

0

Descubrí el último error gracias al aporte de @SchokokuchenBäcker en el primer número. Mi condicional estaba comparando cadenas, ¡por eso 20 era más pequeño que 5! Escribiendo el primer condicional así:

 if (isNaN(minNumChosen) || isNaN(maxNumChosen) || parseInt(minNumChosen) >= parseInt(maxNumChosen) )

lo hace funcional!

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda