...

/

Solution Review: Sum of Digits in a String

Solution Review: Sum of Digits in a String

This review provides a detailed analysis of the solution to find the sum of digits in a string.

We'll cover the following...

Solution: Using Recursion

Press + to interact
function sumDigits(testVariable) {
// Base case
if (testVariable === "") {
return 0;
}
// Recursive case
else {
return Number(testVariable[0]) + sumDigits(testVariable.substr(1));
}
}
// Driver Code
myString = "345";
console.log(sumDigits(myString));

Explanation

The solution to this problem is similar to the solution of finding the ...