Recursion Problem Set (easy)¶
power(n, x)¶
Write a function called
powerwhich accepts a base and an exponent. The function should return the power of the base to the exponent. This function should mimic the functionality ofMath.pow()- do not worry about negative bases and exponents.
1 2 3 4 5 6 | |
factorial(n)¶
Write a function
factorialwhich accepts a number and returns the factorial of that number. A factorial is the product of an integer and all the integers below it; e.g., factorial four (4!) is equal to 24, because 4 * 3 * 2 * 1 equals 24. factorial zero (0!) is always 1.
1 2 3 4 | |
productOfArray(arr)¶
Write a function called
productOfArraywhich takes in an array of numbers and returns the product of them all.
1 2 3 4 | |
recursiveRange(num)¶
Write a function called
recursiveRangewhich accepts a number and adds up all the numbers from 0 to the number passed to the function.
1 2 3 4 | |
fib(num)¶
Write a recursive function called
fibwhich accepts a number and returns the nth number in the Fibonacci sequence. Recall that the Fibonacci sequence is the sequence of whole numbers 1, 1, 2, 3, 5, 8, which starts with 1 and 1, and where every number thereafter is equal to the sum of the previous two numbers.
1 2 3 4 | |