$ ./guess_the_languageProblem Solving — fix the bug
Problem Solving — fix the bug · easy · #56

Which fix removes the subtle bug?

const items = [3, 1, 2];
const sorted = items.sort();
console.log(items);
  1. Copy first: [...items].sort((a,b)=>a-b)
  2. Copy first: [...items].reverse((a,b)=>a-b)
  3. Assign it back with items = items.sort()
  4. Call items.sort() a second time after
sort() mutates the original array in place; spread into a copy to leave items untouched.
Play Problem Solving ▸