Find the sum of the digits of all the numbers from 1 to N (both ends included).

by Igor Cuckovic

JavaScript

function solution (num) {
    var result = 0;
    
    function sumDigits (n) {
        var sum = 0;
        if (n >= 10) {
            sum = (n % 10) + sumDigits(Math.floor(n / 10));
        } else {
            sum = n;
        }
        return sum;
    }
    
    for (var i = 1; i <= num; i += 1) {
        result += sumDigits(i);
    }
    return result;

}
console.log(solution(90))