Count down from 3 to 1 with recursion, collecting the numbers joined by a dash.
Acceptance criteria
- 1.#out should show 3-2-1.
JavaScript Tutorial · JavaScript Functions
These 10 tasks go with the JavaScript Recursion 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 recursion is when a function calls itself to solve a smaller version of the same problem.” .
1.Count downexercise · easy · 10 min
Count down from 3 to 1 with recursion, collecting the numbers joined by a dash.
Passes when: #out should show 3-2-1.
2.Factorialexercise · easy · 10 min · practice plan
Work out factorial of 5 with recursion.
Passes when: #out should show 120.
3.Add up an arrayexercise · easy · 10 min · practice plan
Add every number in the array using recursion.
Passes when: #out should show 10.
4.Count nested itemsexercise · easy · 10 min · practice plan
Count every item in a nested list, however deep it goes.
Passes when: #out should show 6.
5.Reverse a stringexercise · medium · 10 min · practice plan
Reverse the word stream using recursion.
Passes when: #out should show maerts.
6.Fix the wrong base casebug fix · easy · 8 min
The base case returns the wrong starting value.
Passes when: #out should show 120.
7.Fix the value that never shrinksbug fix · easy · 8 min · practice plan
The recursive call passes the same value, so nothing progresses.
Passes when: #out should show 10.
8.Fix the missing recursive callbug fix · easy · 8 min · practice plan
Nested lists are counted as one item instead of being opened up.
Passes when: #out should show 6.
9.Fix the reversed join orderbug fix · easy · 8 min · practice plan
The characters are put back together the wrong way round.
Passes when: #out should show maerts.
10.Fix the base case checked too latebug fix · easy · 8 min · practice plan
The list is read before checking whether it is empty.
Passes when: #out should show 0.
Between them these tasks cover Base case, Recursive case and Nested data.
Looking for a different chapter? See every practice set, or switch to the interview theory questions.