努力将随机数按数字顺序排序
添加了各种排序功能
generateNumbers = () => {
let lottoNum = [];
while (lottoNum.length < 7) {
let random = Math.floor(Math.random() * 59) + 1;
if (lottoNum.indexOf(random) === -1) lottoNum.push(random);
}
return lottoNum.join(" ");
};
let btn = document.getElementById("button");
btn.addEventListener("click", function getNumbers() {
const nums = generateNumbers();
document.getElementById("results").innerHTML = nums;
});
答案 0 :(得分:0)
只需sort
个他们
return lottoNum.sort((a, b) => a - b).join(" ");
降序:
return lottoNum.sort((a, b) => b - a).join(" ");
答案 1 :(得分:0)
要在javascript中对数组进行排序,可以使用sort关键字 试试这个
return lottoNum.sort().join(" ");