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