$ ./guess_the_languageProblem Solving — fix the bug
Problem Solving — fix the bug · hard · #217

Which fix removes the bug?

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