Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

101
Views
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 answers
Answer question

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 Report

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!