I've been faced with the task of creating a random number generator for selecting lottery numbers. The challenge is to generate 6 unique numbers between 1 and 49, in ascending order, without any repeats. Additionally, there is a 7th number called the super-seven, which cannot be any of the previous 6 numbers.
<script type="text/javascript">
const numb = new Array();
for (var i = 0; i < 6; i++) {
numb[i] = Math.floor(49 * Math.random()) + 1;
// compare to existing numbers
for (var k = 0; k < numb.length - 1; k++) {
if (numb[i] == numb[k]) {
i--;
break;
}
}
}
let supNumb = new Array();
supNumb = Math.floor(49 * Math.random()) + 1;
for (var s = 0; s <= 1; s++) {
// compare supNumb to numb
for (var t = 0; t < numb.length - 1; t++) {
if (supNumb == numb[t]) {
s--;
break;
}
}
}
// SORT & DISPLAY NUMBERS
function sort(a, b) {
return a - b;
}
numb.sort(sort);
document.write("<p> " + numb);
document.write("<h4>" + "SuperSeven: " + supNumb);
</script>
After testing, it seems that the super-seven number supNumb
sometimes matches one of the numbers generated in numb
. I'm struggling to figure out how to prevent this from happening.
If anyone could review my code and suggest how I can compare supNumb
to numb
correctly, I would greatly appreciate it.
Is my current approach correct for this task?
Thank you in advance!