So I know this is likely a duplicate, but I have not been able to figure out how to fix my problem after looking through various answers on what seems like the same topic for over an hour.
This function pulls apart the digits in a number, adds them together, and if the resulting number has more than one digit, recursively sens the resulting number back into the function for processing. My return statement is giving me "undefined" and I am not sure why. If it is defined in the if statement, which it is, I do not understand why it would not be so in the else statement:
function digital_root(n) {
var numArray = n.toString().split('');
var accumulator = 0;
for(var i = 0; i < numArray.length; i++){
accumulator += parseInt(numArray[i]);
}
if(accumulator >= 10){
digital_root(accumulator);
}
else{return accumulator};
};
digital_root(942);//should be 6, after 15 is passed back into digital_root()
// but output is undefined.