Javascript Recursion Lab
Objectives
- Practice writing recursive functions
Introducton
Red alert! A rogue web developer is angry and is deleting all of the critical packages that our product depends on! It's our job to rewrite some of the functions that make our app run.
It's important to keep in mind that both of these functions can be implemented iteratively, but writing them that way defeats the point of the exercise. It might be more difficult to figure out exactly how to solve the problems recursively, but do your best to think outside the box.
Function 1
One of the most important functions in our app is the factorial function.
The factorial function takes a number, and returns the product of all
positive integers less than that number. Eg: fact(5) = 5 * 4 * 3 * 2 * 1
Function 2
The second most important function in our app is the sum function.
The sum function takes an array and returns the sum of all its members.
Eg: sum([1,6,9,12,8]) = 1 + 6 + 9 + 12 + 8
Hint: Remember that the sum of the empty array is 0
, the sum of an array
with one element n
is just n
, and the sum of the array [m,n]
is m+n
.
We'll use those as our base cases.