Which fix removes the bug?
function shuffle(arr) {
return arr.sort(() => Math.random() - 0.5);
}
- Use a Fisher-Yates swap loop
- Multiply the random by two
- Copy arr before sorting it
- Return
Math.random() > 0.5
A random comparator breaks sort's assumptions and gives a biased order; Fisher-Yates is uniform.
Play Problem Solving ▸