I'm working on a loop that adds 10 unique random integers from 1-10 to an array, so the expected result would be 2,1,4,6,10,7,9,8,5,3
I manage to generate random numbers and only add the unique ones, my problem is, its not complete 10 items, I think the problem is upon checking I it doesn't generate again.
Hope you help me.
Thanks.
for (let i = 0; i < 10; i++) {
var arrNum = [];
setTimeout(function() {
var r = Math.floor(Math.random() * 10) + 1;
while (arrNum.includes(r) == false) {
arrNum.push(r);
}
if (i == 9) {
$('ul').append('<li>' + arrNum + '</li>');
}
}, 500);
}
ul li{
list-style-type: none;
display: inline-block;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul></ul>