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
I added a few comments to the code. I hope this helps: