📖 Scenario: Imagine you are organizing a treasure hunt game. You have a list of clues, and each clue leads to the next one. You want to find the final treasure by following all clues in order.You can either follow the clues step-by-step (iteration) or ask a friend to help by passing the clues one by one until the last clue is reached (recursion).
🎯 Goal: Build two ways to find the final treasure clue from a list of clues: one using iteration (loop) and one using recursion (function calling itself). Understand when each way is better.
📋 What You'll Learn
Create an array called clues with exact values: 'Start', 'Forest', 'River', 'Mountain', 'Treasure'
Create a variable called steps and set it to 0
Write a function called findTreasureIterative that uses a for loop to find the last clue in clues and counts steps
Write a function called findTreasureRecursive that uses recursion to find the last clue in clues and counts steps
Print the results of both functions and the number of steps taken
💡 Why This Matters
🌍 Real World
Recursion and iteration are ways to solve problems step-by-step or by breaking them into smaller parts. This helps in games, puzzles, and searching tasks.
💼 Career
Understanding when to use recursion or iteration is important for writing efficient code in software development, especially in algorithms and data processing.
Progress0 / 4 steps
1
Create the clues list
Create an array called clues with these exact values in order: 'Start', 'Forest', 'River', 'Mountain', 'Treasure'
DSA Javascript
Hint
Use square brackets [] to create an array and separate values with commas.
2
Create the steps counter
Create a variable called steps and set it to 0 to count how many clues you check
DSA Javascript
Hint
Use let to create a variable that can change later.
3
Write the iterative function
Write a function called findTreasureIterative that uses a for loop with variable i to go through clues. Inside the loop, increase steps by 1. Return the last clue found after the loop.
DSA Javascript
Hint
Use a for loop to count each clue and return the last one.
4
Write the recursive function and print results
Write a function called findTreasureRecursive that takes a parameter index. If index is the last position in clues, return that clue. Otherwise, increase steps by 1 and call findTreasureRecursive with index + 1. Then, print the results of findTreasureIterative(), steps after iteration, reset steps to 0, call findTreasureRecursive(0), and print steps after recursion.
DSA Javascript
Hint
Remember recursion calls itself with the next index until the last clue is found.
Print results using console.log.
Practice
(1/5)
1. Which scenario is best suited for using recursion instead of iteration?
easy
A. When the problem requires simple repeated loops
B. When the problem can be divided into smaller similar subproblems
C. When you want to avoid function call overhead
D. When memory usage must be minimal
Solution
Step 1: Understand recursion use case
Recursion works well when a problem can be broken down into smaller versions of itself, like tree traversals or factorial calculation.
Step 2: Compare with iteration scenarios
Iteration is better for simple repeated tasks without nested subproblems, so recursion is preferred for divide-and-conquer problems.
Final Answer:
When the problem can be divided into smaller similar subproblems -> Option B
Quick Check:
Recursion fits divide-and-conquer = C [OK]
Hint: Recursion fits problems breaking into smaller similar parts [OK]
Common Mistakes:
Thinking iteration is always faster
Ignoring function call overhead in recursion
Confusing simple loops with recursive problems
2. Which of the following is the correct syntax for a recursive function in JavaScript?
easy
A. function recurse(n) { while(n > 0) { n--; } return n; }
B. function recurse(n) { for(let i=0; i
C. function recurse(n) { if (n <= 0) return 0; return n + recurse(n - 1); }
D. function recurse(n) { if(n > 0) { return n * recurse(n + 1); } else { return 1; } }
Solution
Step 1: Identify recursive call and base case
function recurse(n) { if (n <= 0) return 0; return n + recurse(n - 1); } has a base case (n <= 0) and calls itself with a smaller argument (n - 1), which is correct recursion.
Step 2: Check other options for recursion errors
function recurse(n) { while(n > 0) { n--; } return n; } uses a loop, not recursion. function recurse(n) { for(let i=0; i 0) { return n * recurse(n + 1); } else { return 1; } } calls recurse with n + 1, causing infinite recursion.
Final Answer:
function recurse(n) { if (n <= 0) return 0; return n + recurse(n - 1); } -> Option C
Quick Check:
Base case + smaller recursive call = B [OK]
Hint: Look for base case and smaller recursive call [OK]
Common Mistakes:
Missing base case causing infinite recursion
Using loops instead of recursive calls
Increasing argument in recursive call causing stack overflow
3. What is the output of the following JavaScript code?
function factorial(n) {
if (n === 0) return 1;
return n * factorial(n - 1);
}
console.log(factorial(4));