Search⌘ K
AI Features

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

Javascript (babel-node)
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 ...