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

What is the correct fix?

function flatten(arr) {
  return arr.reduce((acc, x) =>
    Array.isArray(x) ? acc.concat(x) : acc.push(x), []);
}
  1. Change the initial value to {} instead
  2. Change reduce to map in the chain
  3. Remove the Array.isArray(x) check
  4. Replace acc.push(x) with acc.concat(x)
push returns a length, not the accumulator; use concat.
Play Problem Solving ▸