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

Which fix removes the bug?

const rows = Array(3).fill([]);
rows[0].push('a');
console.log(rows[1].length);
  1. Build each row: Array.from({length:3}, () => [])
  2. Build each row: Array.from({length:3}, [])
  3. Change fill([]) to fill(new Array())
  4. Push with rows[0] = rows[0].concat('a')
fill puts the SAME array in every slot, so one push shows up in all rows.
Play Problem Solving ▸