Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

108
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda