Skip to main content

JavaScript Array reduce — exercises and bug fixes

5 hands-on exercises and 5 find-the-bug challenges for the JavaScript Array reduce lesson. Every one is checked automatically in the browser — write the code, run it, and the tests tell you what still fails.

Exercises(5)

  1. 1

    Add up a list

    easy10 min

    Use reduce to total a list of numbers.

  2. 2

    Handle an empty array

    easy10 min

    Reduce an empty array with a starting value of zero.

  3. 3

    Find the largest

    easy10 min

    Use reduce to find the biggest number in a list.

  4. 4

    Build a string

    easy10 min

    Use reduce to glue an array of letters into one word.

  5. 5

    Count occurrences

    medium10 min

    Use reduce with an object to count how many times yes appears.

Find the bug(5)

  1. 1

    Fix the missing accumulator return

    easy8 min

    The callback must return the accumulator for the next turn.

  2. 2

    Fix the wrong starting value

    easy8 min

    Starting at one adds an extra to the total.

  3. 3

    Fix the arguments swapped

    easy8 min

    The value is being put before the accumulator, so the word comes out backwards.

  4. 4

    Fix the string starting value

    easy8 min

    Starting with a string turns the addition into text joining.

  5. 5

    Fix the largest comparison

    easy8 min

    The comparison keeps the smaller value instead of the bigger one.

Ready to write the code?

The editor, the automatic checks and the worked solutions open up with an account.

Open the workspace