Viết hàm nhận mảng literal, xáo trộn thứ tự phần tử ngẫu nhiên (in-place).
Ví dụ
Mã minh họa (Fisher-Yates đơn giản):
const letters = ['a', 'b', 'c', 'd', 'e', 'f', 'g'];
const unorderArray = arr => {
let i, pos, temp;
for (i = 0; i < 100; i++) {
pos = Math.random() * arr.length | 0;
temp = arr[pos];
arr.splice(pos, 1);
arr.push(temp);
};
};
unorderArray(letters);
console.log(letters);Kết quả
Đầu ra (một trong nhiều khả năng):
[ 'b', 'e', 'c', 'a', 'g', 'f', 'd' ]