Specific starting values not filling out array

  html, javascript

In my program, I have a form that takes two numbers the user inputs and then creates an array of every number between the two. This works fine for most values, but certain combinations of numbers will fail to fill the array with any numbers at all. The relevant code is as follows:

HTML:

<form id="input">
    <label for="min">Minimum number:</label>
    <input type="text" id="min" name="min" class="field"><br><br>
    <label for="max">Maximum number:</label>
    <input type="text" id="max" name="max" class="field"><br><br>
</form>

JS:

let min = document.getElementById("min").value;
let max = document.getElementById("max").value;
let range = [];
while(min <= max) {
        range.push(min++);
    };

This works perfectly fine for a lot of ranges like 1-10, 1-100, or 10-20, but fails if a range like 3-10 or 5-10 is input. Through my troubleshooting so far, I’ve concluded that it’s not any one min or max value that breaks, but instead just certain arbitrary combinations of numbers. It’s also 100% consistent and reproduceable, if that means anything.

Any help or insight into what I’ve done wrong would be appreciated 🙂

Source: Ask Javascript Questions

LEAVE A COMMENT