使用for循环对数组进行分组
<script>
let names = new Array();
for (let index = 0; index < 34; index++) {
if (index < 9) {
names[index] = `user0${index + 1}`;
} else {
names[index] = `user${index + 1}`;
}
}
let personCount = 5;
let count = Math.ceil(names.length / personCount);
let groups = new Array();
for (let index = 0; index < count; index++) {
let group = new Array();
if (personCount < names.length) {
for (let index = 0; index < personCount; index++) {
group.push(names.splice(Math.floor(Math.random() * names.length), 1)[0]);
}
groups.push(group);
} else {
if (names.length < groups.length) {
let len = names.length;
for (let j = 0; j < len; j++) {
groups[j].push(names.splice(0, 1)[0]);
}
} else {
groups.push(names);
}
}
}
console.log(groups);
</script>

使用for循环模拟抽奖
<script>
let names = new Array();
for (let index = 0; index < 33; index++) {
if (index < 9) {
names[index] = `user0${index + 1}`;
} else {
names[index] = `user${index + 1}`;
}
}
for (let index = 0; index < 3; index++) {
document.writeln('三等奖 >>> ' + names.splice(Math.floor(Math.random() * names.length), 1)[0] + '<br>');
}
for (let index = 0; index < 2; index++) {
document.writeln('二等奖 >>> ' + names.splice(Math.floor(Math.random() * names.length), 1)[0] + '<br>');
}
document.writeln('一等奖 >>> ' + names.splice(Math.floor(Math.random() * names.length), 1)[0] + '<br>');
</script>
