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...
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; } 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).
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!