Recursive Digit Sum

  • + 0 comments
    const sumDigits = (n) => String(n).split('').reduce((acc, cur) => acc + Number(cur), 0)
    function superDigit(n, k) {
        if (n < 10) {
            const value = sumDigits(n * k)
            const output = value < 10 ? value : superDigit(value, 1)
            return output
        } else {
            const step = sumDigits(n)
            return superDigit(step, k)
        }
    }