📖 Scenario: Imagine you have a list of words from a story. You want to find out if a certain word appears in the list using a method that checks one word at a time by calling itself again and again until it finds the word or finishes the list.
🎯 Goal: Build a recursive function in JavaScript that checks if a word exists in an array of words by looking at one word at a time.
📋 What You'll Learn
Create an array called words with the exact values: ['apple', 'banana', 'cherry', 'date', 'fig']
Create a variable called targetWord and set it to the string 'cherry'
Write a recursive function called findWord that takes two parameters: arr (array) and index (number)
The function should return true if targetWord is found in arr, otherwise false
Use recursion to check each word starting from index 0
Print the result of calling findWord(words, 0)
💡 Why This Matters
🌍 Real World
Searching for items in lists is common in apps like contact lists, shopping carts, or word games.
💼 Career
Understanding recursion helps in solving problems that involve breaking down tasks into smaller parts, useful in coding interviews and algorithm design.
Progress0 / 4 steps
1
Create the array of words
Create an array called words with these exact values: 'apple', 'banana', 'cherry', 'date', 'fig'
DSA Javascript
Hint
Use square brackets [] to create an array and separate words with commas.
2
Set the target word to find
Create a variable called targetWord and set it to the string 'cherry'
DSA Javascript
Hint
Use const to create a variable and assign the string 'cherry'.
3
Write the recursive function to find the word
Write a recursive function called findWord that takes parameters arr and index. It should return true if targetWord is found at arr[index] or in the rest of the array by calling itself with index + 1. Return false if index is beyond the last element.
DSA Javascript
Hint
Check if index is outside the array to stop recursion. Compare arr[index] with targetWord. Call findWord again with index + 1 if not found.
4
Print the result of the recursive search
Print the result of calling findWord(words, 0) using console.log
DSA Javascript
Hint
Use console.log to show the result of findWord(words, 0).
Practice
(1/5)
1. What does a recursive function on an array typically do to process the array elements?
easy
A. It uses loops instead of calling itself.
B. It processes one element and calls itself on the rest of the array.
C. It processes all elements at once without calling itself.
D. It only processes the first and last elements.
Solution
Step 1: Understand recursion on arrays
Recursive functions handle one element and then call themselves on the smaller part (rest of the array).
Step 2: Identify the typical recursive pattern
This pattern reduces the problem size by one element each call until the array is empty.
Final Answer:
It processes one element and calls itself on the rest of the array. -> Option B
Quick Check:
Recursion = process one + recurse rest [OK]
Hint: Recursion breaks problem into smaller parts [OK]
Common Mistakes:
Thinking recursion processes all elements at once
Confusing recursion with loops
Ignoring the base case
2. Which of the following is the correct syntax to define a recursive function in JavaScript that takes an array as input?
easy
A. function recurse(arr) { if(arr.length === 0) return; recurse(arr.slice(1)); }
B. function recurse(arr) { while(arr.length > 0) recurse(arr); }
C. function recurse(arr) { for(let i=0; i
D. function recurse(arr) { if(arr.length === 0) return; recurse(arr.pop()); }
Solution
Step 1: Check base case and recursive call
function recurse(arr) { if(arr.length === 0) return; recurse(arr.slice(1)); } correctly checks if array is empty and calls itself with the rest of the array using slice(1).
Step 2: Identify incorrect syntax in other options
The version using while(arr.length > 0) recurse(arr); causes infinite recursion because it passes the same array; the version using for loop calls recurse on individual elements; the version using arr.pop() passes a single element, not an array.
Final Answer:
function recurse(arr) { if(arr.length === 0) return; recurse(arr.slice(1)); } -> Option A
Quick Check:
Base case + recurse smaller array = correct syntax [OK]
Hint: Base case + smaller input slice needed [OK]
Common Mistakes:
Missing base case causing infinite recursion
Passing wrong argument type in recursive call
Using loops inside recursion incorrectly
3. What will be the output of the following code?
function sumArray(arr) {
if (arr.length === 0) return 0;
return arr[0] + sumArray(arr.slice(1));
}
console.log(sumArray([1, 2, 3, 4]));
medium
A. 1234
B. Error
C. undefined
D. 10
Solution
Step 1: Understand the recursive sum logic
The function adds the first element to the sum of the rest recursively until array is empty.