Recursion Practice Problems
Below are some problems that you might want to try. After writing each function, try testing it by
calling the function with various combinations of arguments. If you’re having trouble, drop by to see
one of the TAs during office hours.
1. Write a function that uses recursion to return a String containing all of the whole numbers in the
range from a to b:
function getValuesInRange(a, b)
For example, if a is 5 and b is 10 then the return value should be “5 6 7 8 9 10 “.
2. Write a function that uses recursion to return the product of two non-negative whole numbers. But…
You may not use the multiplication operator (*) and you may not use the division operator (/). Hint:
You need to think of multiplication as repeated addition.
function multiply(x, y)
For example, if x is 5 and y is 7 then the return value should be 35.
3. Write a function (using recursion) that works as follows: Assume the parameter, arr, is an array of
numerical values and that cutoff is a whole number. The function will return the sum of the values in
the array whose indices are greater than or equal to cutoff.
function partSum(arr, cutoff)
For example, if arr is the array [12, 4, 105, 6, 8, 7] and cutoff is 3, then the return value should be 21,
because 6 + 8 + 7 = 21. (The elements with indices greater than or equal to the cutoff, 3, would be 6, 8,
and 7.)