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

103
Visualizações
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 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;

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

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

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