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

102
Vistas
Page displays formula rather than result (but for only ONE of the cases)

I'm a student currently learning JavaScript. As practice, I wanted to make a cute reading randomizer for a friend with a simple form and a if else input validation process. Both of my first two cases function as I expect them two, but the third, the one that actually does the calculation, does not send the result of the calculation to be displayed, but rather the formula. I'm not sure where I went wrong.

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;
}

For the last case, the page displays : "Today, you should read fic number (minNumChosen, maxNumChosen) => { return Math.floor(Math.random() * (max - min) + min); }!"

You can find the codepen here.

EDIT: Turns out I found another bug, which is probably logic based. It seems that for my function, 2 is greater than 10. So it must be judging by the first digit...

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;

OR

You created a function which takes in values but you didn't provide the min and max value while calling the generateRandomNumber function

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>

You had to add parenthesis to generateRandomNumber()

And also make the minNumChosen and maxNumChosen into integers with parseInt().

There was also another mistake where you didn't name the parameters of your generateRandomNumber function (min, max).

about 4 years ago · Juan Pablo Isaza Denunciar

0

I figured out the last bug thanks to @SchokokuchenBäcker's input on the first issue. My conditional was comparing strings, which is why 20 was smaller than 5 ! Writing the first conditional like this:

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

makes it functional!

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