can someone explain to me this code? why it has -1 at the end?

64 Views Asked by At

this code takes input to define a range, then shows 6 numbers when the button is clicked. i dont understand why there is a -1 at the var digits = digits - 1. can you explain it to me? i'm just new here, i hope everybody understands

function randomize() {
  var min = Number(document.getElementById('inputmin').value);
  var max = Number(document.getElementById('inputmax').value);
  var digits = 6
  var inside = Number(max - min);

  if (min > max) {
    document.getElementById('answer').innerHTML = "please make the maximum number higher than the minimum";
  } else if (min == "" && max == "") {
    document.getElementById('answer').innerHTML = "lacking numbers";
  } else if (min == "") {
    document.getElementById('answer').innerHTML = "lacking minimum number";
  } else {

    myArray = [];

    var text = "";
    do {
      var random = Math.floor(Math.random() * (inside)) + min;
      var noRep = myArray.includes(random);
      if (noRep == false) {
        myArray.push(random);
        text += " | " + random + " | ";
        var digits = digits - 1;
      }
    }
    while (digits > 0);
    document.getElementById('answer').innerHTML = text;
  }
}
<html>

<head>
  <title>NO.1</title>

</head>

<body>

  <h1>
    <p>Enter Minimum Number</p>
    <input type="number" id="inputmin">
  </h1>
  <h1>
    <p>Enter Maximum Number</p>
    <input type="number" id="inputmax">
  </h1>
  <h1>
    <button type="button" onclick='randomize()'>Randomize!</button>
  </h1>


  <br/>
  <h1 id="answer"></h1>
  <br/>

</body>

</html>

i tried putting -2 and and it only shows 4 digits, i dont understand why

1

There are 1 best solutions below

1
On

I added a few comments to the code. I hope this helps:

function randomize() {
    // Get the min and max values from the input fields
    var min = Number(document.getElementById("inputmin").value);
    var max = Number(document.getElementById("inputmax").value);

    // Make sure the min is less than the max
    if (min > max) {
        document.getElementById("answer").innerHTML = "please make the maximum number higher than the minimum";

        return;
    }

    // Make sure min and max are not empty
    if (min == "" && max == "") {
        document.getElementById("answer").innerHTML = "lacking numbers";

        return;
    } else if (min == "") {
        document.getElementById("answer").innerHTML = "lacking minimum number";

        return;
    }

    // Get the number of digits to generate
    var digits = 6;

    // Create an array to store the generated numbers
    var myArray = [];

    // Generate the random numbers
    var text = "";

    // Loop until we have generated the required number of digits
    do {
        var random = Math.floor(Math.random() * (max - min)) + min;
        var noRep = myArray.includes(random);

        // Make sure the generated number is not a duplicate
        if (noRep == false) {
            // Add the number to the array
            myArray.push(random);
            // Add the number to the text to display
            text += " | " + random + " | ";
            // Decrement the number of digits to generate by 1 so we know when to stop
            var digits = digits - 1;
        }
    } while (digits > 0);

    // Display the generated numbers
    document.getElementById("answer").innerHTML = text;
}