Why Recursion Exists and What Loops Cannot Express Cleanly
📖 Scenario: Imagine you have a set of nested boxes, each box can contain another box inside it, and so on. You want to find the smallest box inside all these nested boxes.
🎯 Goal: You will build a simple program that uses recursion to find the smallest number inside nested arrays. This shows why recursion is useful when loops alone cannot easily handle nested or self-similar structures.
📋 What You'll Learn
Create a nested array called nestedBoxes with exact values
Create a variable called smallest to hold the smallest number found
Write a recursive function called findSmallest that takes an array and updates smallest
Call findSmallest on nestedBoxes
Print the value of smallest
💡 Why This Matters
🌍 Real World
Nested data structures appear in file systems, menus, organizational charts, and more. Recursion helps process these naturally.
💼 Career
Understanding recursion is key for software developers working with trees, graphs, and complex data formats like JSON or XML.
Progress0 / 4 steps
1
Create the nested data structure
Create a variable called nestedBoxes and set it to this exact nested array: [5, [3, [2, 8], 4], 7]
Create a variable called smallest and set it to Infinity to start with the largest possible number.
DSA Javascript
Hint
Use let smallest = Infinity; to start.
3
Write the recursive function to find the smallest number
Write a function called findSmallest that takes a parameter arr. Inside, use a for loop with variable item to check each element. If item is an array, call findSmallest(item) recursively. Otherwise, if item is less than smallest, update smallest to item.
DSA Javascript
Hint
Use recursion by calling findSmallest inside itself when item is an array.
4
Call the function and print the smallest number
Call findSmallest with nestedBoxes as argument. Then print the value of smallest using console.log(smallest).
DSA Javascript
Hint
Call findSmallest(nestedBoxes) then print smallest.
Practice
(1/5)
1. Why do we use recursion instead of loops in some problems? function factorial(n) { if (n === 0) return 1; return n * factorial(n - 1); }
easy
A. Loops are always slower than recursion.
B. Recursion can express problems with self-similar structure more naturally.
C. Recursion uses less memory than loops.
D. Loops cannot perform repeated actions.
Solution
Step 1: Understand recursion's purpose
Recursion solves problems by breaking them into smaller similar problems, like factorial breaking n! into n * (n-1)!.
Step 2: Compare recursion and loops
Loops repeat actions but do not naturally express self-similar or nested structures, which recursion handles clearly.
Final Answer:
Recursion can express problems with self-similar structure more naturally. -> Option B
Quick Check:
Recursion = natural for self-similar problems [OK]
Hint: Recursion fits problems breaking into smaller similar parts [OK]
Common Mistakes:
Thinking loops are always slower
Believing recursion uses less memory
Assuming loops cannot repeat
2. Which of the following is the correct syntax to define a recursive function in JavaScript?
easy
A. function recurse(n) { for(let i=0; i
B. function recurse(n) { while(n > 0) recurse(n - 1); }
C. function recurse(n) { if (n > 0) return n; }
D. function recurse(n) { if (n <= 0) return; recurse(n - 1); }
Solution
Step 1: Identify recursive call structure
function recurse(n) { if (n <= 0) return; recurse(n - 1); } calls itself with a smaller argument and has a base case to stop recursion.
Step 2: Check other options for recursion correctness
The options using for or while loops include recursive calls but lack base cases, leading to infinite recursion. The option that returns n without a recursive call is not recursive.
Final Answer:
function recurse(n) { if (n <= 0) return; recurse(n - 1); } -> Option D
Quick Check:
Correct recursion has base case and self-call [OK]
Hint: Look for base case and self-call in function [OK]
Common Mistakes:
Missing base case causing infinite recursion
Using loops instead of recursion
No recursive call inside function
3. What is the output of this recursive function?
function sumTo(n) { if (n === 0) return 0; return n + sumTo(n - 1); } console.log(sumTo(3));