<div id="app">
<button id="btn">点名</button>
</div>
<script>
let count = 0//执行次数
let arr = ["张三","李四","王五","赵六","xxx"]
let btn = document.getElementById("btn")
btn.onclick = function(){
count = 0
console.log(genRandomName(arr))
}
function genRandomName(arr) {
let str = "";
let charIndex = Math.floor(Math.random() * arr.length);
if (count < 3) {
count++;
genRandomName(arr);
}
return charIndex + ":" + arr[charIndex];
}
</script>
产生随机数,在根据随机数匹配数组下标
Math.random() *5 然后 arr[Math.random() *5] 配合for循环