📖 Scenario: You want to find the nth number in the Fibonacci sequence. The Fibonacci sequence starts with 0 and 1, and each next number is the sum of the two before it.This sequence is like counting rabbits: each pair produces another pair, so the numbers grow step by step.
🎯 Goal: Build a small program that uses a recursive function to find the Fibonacci number at position n.You will first set the position number, then write the recursive function, and finally print the result.
📋 What You'll Learn
Create a variable n with the value 7
Create a recursive function fibonacci that takes one number parameter num
The function should return 0 if num is 0
The function should return 1 if num is 1
Otherwise, the function should return the sum of fibonacci(num - 1) and fibonacci(num - 2)
Print the result of fibonacci(n)
💡 Why This Matters
🌍 Real World
Fibonacci numbers appear in nature, computer algorithms, and financial models. Understanding recursion helps solve problems that break down into smaller similar problems.
💼 Career
Many programming jobs require understanding recursion and sequence calculations, useful in algorithm design, coding interviews, and problem solving.
Progress0 / 4 steps
1
Set the Fibonacci position
Create a variable called n and set it to 7.
DSA Javascript
Hint
Use const n = 7; to store the position number.
2
Write the recursive Fibonacci function
Create a function called fibonacci that takes one parameter num. Inside the function, return 0 if num is 0, and return 1 if num is 1.
DSA Javascript
Hint
Use if statements to check if num is 0 or 1 and return the correct value.
3
Add the recursive call to calculate Fibonacci
Inside the fibonacci function, after the if checks, return the sum of fibonacci(num - 1) and fibonacci(num - 2).
DSA Javascript
Hint
Use return fibonacci(num - 1) + fibonacci(num - 2); to get the sum of the two previous Fibonacci numbers.
4
Print the Fibonacci number at position n
Write a console.log statement to print the result of fibonacci(n).
DSA Javascript
Hint
Use console.log(fibonacci(n)); to show the Fibonacci number for n.
Practice
(1/5)
1. What does a recursive Fibonacci function do when called with fib(4)?
easy
A. Throws a syntax error because recursion is not allowed
B. Returns 4 immediately without any calculation
C. Runs an infinite loop without returning
D. Calculates the 4th Fibonacci number by calling itself with smaller numbers
Solution
Step 1: Understand recursive calls for fib(4)
The function calls fib(3) and fib(2) to calculate fib(4).
Step 2: Combine results of smaller calls
fib(3) and fib(2) are calculated recursively and their sum is returned as fib(4).
Final Answer:
Calculates the 4th Fibonacci number by calling itself with smaller numbers -> Option D
Quick Check:
Recursive calls break problem into smaller parts [OK]
Hint: Recursion breaks problem into smaller calls [OK]
Common Mistakes:
Thinking recursion returns input directly
Assuming recursion causes infinite loop always
Confusing recursion with iteration
2. Which of the following is the correct base case syntax for a recursive Fibonacci function in JavaScript?
easy
A. if (n = 0) return 0;
B. if (n <= 1) return n;
C. if n == 1 then return 1
D. if (n > 1) return n;
Solution
Step 1: Identify correct JavaScript syntax for base case
The base case must check if n is 0 or 1 and return n using proper JavaScript syntax.
Step 2: Validate each option
if (n <= 1) return n; uses correct syntax: if (n <= 1) return n; Options A and B have syntax errors or wrong operators, D is logically incorrect.
Final Answer:
if (n <= 1) return n; -> Option B
Quick Check:
Base case uses if (n <= 1) return n; [OK]
Hint: Base case returns n when n is 0 or 1 [OK]
Common Mistakes:
Using assignment (=) instead of comparison (== or ===)
Missing parentheses in if condition
Incorrect logical condition for base case
3. What is the output of the following code?
function fib(n) {
if (n <= 1) return n;
return fib(n - 1) + fib(n - 2);
}
console.log(fib(5));
medium
A. 5
B. 8
C. 3
D. 13
Solution
Step 1: Trace fib(5) recursive calls
fib(5) = fib(4) + fib(3), fib(4) = fib(3) + fib(2), fib(3) = fib(2) + fib(1), and so on until base cases.