loading...

re: Solving the Digital Root Algorithm using JavaScript VIEW POST

FULL DISCUSSION
 

Are we sure this is a recursive approach? I feel like this is an iterative solution as we just run a block until a condition is met. The function is not called multiple times.

function dRoot(num) {
  const parts = num.toString().split("");
  const sum = parts.reduce((aggr,part) => parseInt(part) + parseInt(aggr));
  if(sum > 9) return dRoot(sum);
  return sum;
}

I think this qualifies as recursive. But the iterative approach in the article is much more performant I think. Nice article.

code of conduct - report abuse