📖 Scenario: Imagine you have a list of daily expenses and you want to find out the total amount spent. Instead of adding them one by one using a loop, you will use a simple method called recursion to add all the numbers.
🎯 Goal: Build a TypeScript program that uses recursion to calculate the sum of all numbers in an array.
📋 What You'll Learn
Create an array called expenses with exact numbers: 100, 200, 300, 400, 500
Create a variable called length that stores the length of the expenses array
Write a recursive function called sumArray that takes the array and its length as parameters and returns the sum of the elements
Print the result of calling sumArray(expenses, length)
💡 Why This Matters
🌍 Real World
Recursion helps solve problems where a task can be broken down into smaller similar tasks, like calculating totals, searching, or sorting.
💼 Career
Understanding recursion is important for coding interviews and solving complex problems efficiently in software development.
Progress0 / 4 steps
1
Create the expenses array
Create an array called expenses with these exact numbers: 100, 200, 300, 400, 500
DSA Typescript
Hint
Use square brackets [] to create the array and separate numbers with commas.
2
Create the length variable
Create a variable called length that stores the length of the expenses array
DSA Typescript
Hint
Use the length property of the array to get its size.
3
Write the recursive sumArray function
Write a recursive function called sumArray that takes two parameters: arr (an array of numbers) and n (a number). The function should return the sum of the first n elements of arr. Use the base case when n is 0 to return 0, otherwise return the last element plus the sum of the rest.
DSA Typescript
Hint
Think of the function calling itself with one less element until it reaches zero.
4
Print the sum of the expenses array
Print the result of calling sumArray(expenses, length) using console.log
DSA Typescript
Hint
Use console.log to show the final sum on the screen.
Practice
(1/5)
1. What does a recursive function on an array typically do?
easy
A. It only works on strings, not arrays.
B. It loops through the array without calling itself.
C. It creates a new array without using recursion.
D. It calls itself with a smaller part of the array until a base case is reached.
Solution
Step 1: Understand recursion on arrays
Recursive functions on arrays call themselves with smaller parts of the array to solve the problem step-by-step.
Step 2: Identify the base case
The recursion stops when the array is empty or has one element, which is the base case.
Final Answer:
It calls itself with a smaller part of the array until a base case is reached. -> Option D
Quick Check:
Recursion = smaller parts + base case [OK]
Hint: Recursion breaks problem into smaller parts until simple case [OK]
Common Mistakes:
Thinking recursion loops without calling itself
Confusing recursion with iteration
Believing recursion only works on strings
2. Which of the following is the correct TypeScript syntax to define a recursive function that sums elements of a number array?
easy
A. function sum(arr: number[]): number { return arr[0] + sum(arr.slice(1)); }
B. function sum(arr: number[]): number { if(arr.length === 0) return 1; return arr[0] + sum(arr.slice(1)); }
C. function sum(arr: number[]): number { if(arr.length === 0) return 0; return arr[0] + sum(arr.slice(1)); }
D. function sum(arr: number[]): number { if(arr.length === 0) return 0; return sum(arr.slice(1)); }
Solution
Step 1: Check base case correctness
The base case should return 0 when array is empty to stop recursion correctly.
Step 2: Check recursive call and addition
The function adds the first element to the sum of the rest of the array recursively.
Final Answer:
function sum(arr: number[]): number { if(arr.length === 0) return 0; return arr[0] + sum(arr.slice(1)); } -> Option C
Quick Check:
Base case returns 0 + recursive sum [OK]
Hint: Base case returns 0; recursive call adds first element [OK]
Common Mistakes:
Missing base case causing infinite recursion
Returning wrong base case value like 1
Not adding the first element before recursive call
3. What is the output of this TypeScript code?
function reverseString(s: string): string {
if (s === "") return "";
return reverseString(s.slice(1)) + s[0];
}
console.log(reverseString("abc"));