Hello is it possible to sort these random generated numbers in ascending order? I create randon 40 numbers out of 100 which are unique but when i try to sort them they are always out of order in the boxes. I tried sort or bubling or adding length but the result is always same. I know this is very basic but maybe there is something missing with my code... here is the final setup i tried...
var randomNums = [];
randomNums.sort(function (a,b) {
return b - a;
})
for(var i = 0; i < 40 ; i++) {
var temp = Math.floor(Math.random() * 100);
if (randomNums.indexOf(temp) == -1) {
randomNums.push(temp);
document.getElementById("box"+i).innerHTML = randomNums [i];
}
else {
i--;
}
}
}```
sorting function down, after yourforloop. You are sorting an empty array.return b - awill sort in descending order, if you want ascending order you shouldreturn a - bsorting function down, after yourforloop. You are sorting an empty array.b - awould yield a descending order, usea - b. Also you have an extra closing brace.