Use reduce to total a list of numbers.
Acceptance criteria
- 1.#out should show 60.
JavaScript Tutorial · JavaScript Arrays
These 10 tasks go with the JavaScript Array reduce chapter. 5 of them ask you to build something small from scratch; 5 give you code that is already broken and ask you to work out why. Set aside about 90 minutes for the whole set, though you can do them in any order.
Nothing here is marked by a person. When you press Submit, the editor checks your code against the points listed under each task and tells you straight away what passed and what didn't (you need a free account to submit). If you get stuck, there are hints, and a worked solution once you've had a go. The first 2 tasks are free; the rest are part of the practice plan.
If it's been a while since you read the chapter, here is how it starts: “JavaScript reduce boils an array down to a single value.” .
1.Add up a listexercise · easy · 10 min
Use reduce to total a list of numbers.
Passes when: #out should show 60.
2.Handle an empty arrayexercise · easy · 10 min · practice plan
Reduce an empty array with a starting value of zero.
Passes when: #out should show 0.
3.Find the largestexercise · easy · 10 min · practice plan
Use reduce to find the biggest number in a list.
Passes when: #out should show 44.
4.Build a stringexercise · easy · 10 min · practice plan
Use reduce to glue an array of letters into one word.
Passes when: #out should show code.
5.Count occurrencesexercise · medium · 10 min · practice plan
Use reduce with an object to count how many times yes appears.
Passes when: #out should show 2.
6.Fix the missing accumulator returnbug fix · easy · 8 min
The callback must return the accumulator for the next turn.
Passes when: #out should show 60.
7.Fix the wrong starting valuebug fix · easy · 8 min · practice plan
Starting at one adds an extra to the total.
Passes when: #out should show 60.
8.Fix the arguments swappedbug fix · easy · 8 min · practice plan
The value is being put before the accumulator, so the word comes out backwards.
Passes when: #out should show code.
9.Fix the string starting valuebug fix · easy · 8 min · practice plan
Starting with a string turns the addition into text joining.
Passes when: #out should show 60.
10.Fix the largest comparisonbug fix · easy · 8 min · practice plan
The comparison keeps the smaller value instead of the bigger one.
Passes when: #out should show 44.
Between them these tasks cover reduce, Accumulators and Starting values.
Looking for a different chapter? See every practice set, or switch to the interview theory questions.