更新時(shí)間:2021年12月22日13時(shí)42分 來(lái)源:傳智教育 瀏覽次數(shù):
<script> var arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; function randSort1(arr) { for (var i = 0, len = arr.length; i < len; i++) { var rand = parseInt(Math.random() * len); var temp = arr[rand]; arr[rand] = arr[i]; arr[i] = temp; } return arr; } console.log(randSort1(arr)); </script>
<script> var arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; function randSort2(arr) { var mixedArray = []; while (arr.length > 0) { var randomIndex = parseInt(Math.random() * arr.length); mixedArray.push(arr[randomIndex]); arr.splice(randomIndex, 1); } return mixedArray; } console.log(randSort2(arr)); </script>
<script> var arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; arr.sort(function () { return Math.random() - 0.5; }) console.log(arr); </script>
猜你喜歡:
JS如何實(shí)現(xiàn)鼠標(biāo)懸浮時(shí)表格對(duì)應(yīng)行變色?
北京校區(qū)