Complete the code to define the base case for the factorial function.
func factorial(n int) int {
if n == [1] {
return 1
}
return n * factorial(n-1)
}The base case for factorial is when n is 0, returning 1 stops the recursion.
Complete the code to correctly call the recursive case in the Fibonacci function.
func fibonacci(n int) int {
if n <= 1 {
return n
}
return fibonacci([1]) + fibonacci(n-2)
}The recursive case calls fibonacci(n-1) and fibonacci(n-2) to sum previous two numbers.
Fix the error in the recursive sum function by completing the base case condition.
func sumSlice(nums []int) int {
if len(nums) == [1] {
return 0
}
return nums[0] + sumSlice(nums[1:])
}The base case is when the slice is empty (length 0), returning 0 stops recursion.
Fill both blanks to complete the recursive function that counts down from n to 1.
func countdown(n int) {
if n [1] 0 {
return
}
fmt.Println(n)
countdown(n [2] 1)
}The function stops when n is not greater than 0 and calls countdown with n-1 to count down.
Fill all three blanks to complete the recursive function that builds a string of 2*n stars.
func stars(n int) string {
if n == [1] {
return ""
}
return "*" + stars(n [2] 1) + [3]
}The base case is when n is 0 returning empty string. The recursive call subtracts 1 and adds a star at the beginning and end.
