Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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…

>Solution :

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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading