Solution Review: Length of a String
Explore how to calculate the length of a string using recursion in JavaScript. This lesson helps you understand the base and recursive cases to solve string length problems recursively, building skills for coding interviews.
We'll cover the following...
Solution: Using Recursion
Explanation
In Javascript it is preferable to use
.lengthfunction for finding the length of a string. However, this exercise is meant as practice. Here we teach how to find the length of a string recursively. Also, this concept can be used on various other problems that involve counting something in a string.
The base case for this solution (line number 3 to 5) will test for an empty string "". If the string is empty we return .
For the recursive case, (line number 9), we call another instance of the same function, but we remove the first element. When the child element returns the length, we add to the ...