$ ./guess_the_languageProblem Solving — fix the bug
Problem Solving — fix the bug · medium · #96

What is the correct fix?

const ok = [1, 2, 3].reduce((a, b) => a + b);
const bad = [].reduce((a, b) => a + b);
  1. Give a seed: .reduce((a, b) => a + b, 0)
  2. Give a seed: .reduce(0, (a, b) => a + b)
  3. Guard with if (arr.length > 1) first
  4. Change reduce to reduceRight here
reduce on an empty array without an initial value throws a TypeError.
Play Problem Solving ▸