...

/

Solution Review: Length of a String

Solution Review: Length of a String

This review provides a detailed analysis of the solution to find the length of a string recursively.

We'll cover the following...

Solution: Using Recursion

Press + to interact
function recursiveLength(testVariable) {
// Base case
if (testVariable === "") {
return 0;
}
// Recursive case
else {
return 1 + recursiveLength(testVariable.substr(1));
}
}
// Driver Code
testVariable = "Educative";
console.log(recursiveLength(testVariable));

Explanation

In Javascript it is preferable to use .length function 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 ...