📖 Scenario: You want to calculate the Fibonacci number at a certain position. The Fibonacci sequence starts with 0 and 1, and each next number is the sum of the two before it.Imagine you are building a simple calculator that finds the Fibonacci number for a given position using a method called recursion, where the function calls itself.
🎯 Goal: Build a recursive function called fibonacci that takes a number n and returns the Fibonacci number at position n.Then, use this function to find the Fibonacci number at position 6 and print the result.
📋 What You'll Learn
Create a variable n with the value 6.
Create a recursive function called fibonacci that takes a number num and returns the Fibonacci number at that position.
Use the base cases: if num is 0, return 0; if num is 1, return 1.
For other cases, return the sum of fibonacci(num - 1) and fibonacci(num - 2).
Call the fibonacci function with n and store the result in a variable called result.
Print the value of result.
💡 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
Recursion is a key concept in programming interviews and algorithm design. Knowing how to write and understand recursive functions is important for software development roles.
Progress0 / 4 steps
1
Create the input variable
Create a variable called n and set it to the number 6.
DSA Typescript
Hint
Use const n = 6; to create the variable.
2
Write the recursive Fibonacci function
Create a function called fibonacci that takes a parameter num of type number. Inside the function, write these rules: - If num is 0, return 0. - If num is 1, return 1. - Otherwise, return fibonacci(num - 1) + fibonacci(num - 2).
DSA Typescript
Hint
Use function fibonacci(num: number): number { ... } and the base cases with if statements.
3
Call the function and store the result
Call the fibonacci function with the variable n and store the returned value in a variable called result.
DSA Typescript
Hint
Use const result = fibonacci(n); to call the function and save the answer.
4
Print the Fibonacci result
Print the value of the variable result using console.log.
DSA Typescript
Hint
Use console.log(result); to show the answer.
Practice
(1/5)
1. What does the Fibonacci sequence represent when calculated using recursion?
easy
A. Each number is the sum of the two preceding numbers
B. Each number is the product of the two preceding numbers
C. Each number is the difference of the two preceding numbers
D. Each number is the square of the previous number
Solution
Step 1: Understand Fibonacci sequence definition
The Fibonacci sequence is defined such that each number is the sum of the two numbers before it.
Step 2: Recognize recursion use
Recursion calls the function itself to calculate previous two numbers and sums them.
Final Answer:
Each number is the sum of the two preceding numbers -> Option A
Quick Check:
Fibonacci = sum of two previous numbers [OK]
Hint: Fibonacci adds last two numbers, not multiply or subtract [OK]
Common Mistakes:
Thinking Fibonacci multiplies previous numbers
Confusing subtraction or squares with addition
Assuming Fibonacci is a product sequence
2. Which of the following is the correct TypeScript function signature for a recursive Fibonacci function?
easy
A. function fib(n: string): string
B. function fib(n: boolean): boolean
C. function fib(n: number): number
D. function fib(): void
Solution
Step 1: Identify input and output types
The Fibonacci function takes a number input and returns a number output.
Step 2: Match correct function signature
Only the signature with number input and number output matches the Fibonacci function requirements.
Final Answer:
function fib(n: number): number -> Option C
Quick Check:
Input and output types must be number [OK]
Hint: Fibonacci input/output are numbers, not strings or booleans [OK]
Common Mistakes:
Using wrong parameter types like string or boolean
Missing return type or using void
Confusing function signature syntax
3. What is the output of the following TypeScript code?
function fib(n: number): number {
if (n <= 1) return n;
return fib(n - 1) + fib(n - 2);
}
console.log(fib(4));