The following javascript function only returns undefined
function digital_root(n) {
var x = String(n).split('');
if (x.length > 1) {
sum = x.reduce((accumulator, value) => parseInt(value) + accumulator, 0);
digital_root(sum);
} else {
console.log(x);
return x;
}
}
however it prints the correct value when run in node. So why does it return undefined?
> digital_root(12)
[ '3' ]
undefined
If I take the console.log(x) statement out, the function still returns undefined
> digital_root(12)
undefined