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
Add up a list
easy10 minUse reduce to total a list of numbers.
- 2
Handle an empty array
easy10 minReduce an empty array with a starting value of zero.
- 3
Find the largest
easy10 minUse reduce to find the biggest number in a list.
- 4
Build a string
easy10 minUse reduce to glue an array of letters into one word.
- 5
Count occurrences
medium10 minUse reduce with an object to count how many times yes appears.
Find the bug(5)
- 1
Fix the missing accumulator return
easy8 minThe callback must return the accumulator for the next turn.
- 2
Fix the wrong starting value
easy8 minStarting at one adds an extra to the total.
- 3
Fix the arguments swapped
easy8 minThe value is being put before the accumulator, so the word comes out backwards.
- 4
Fix the string starting value
easy8 minStarting with a string turns the addition into text joining.
- 5
Fix the largest comparison
easy8 minThe 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