Find the sum of the digits

by Igor Cuckovic

JavaScript

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

For N = 10 the sum is 1+2+3+4+5+6+7+8+9+(1+0) = 46
For N = 11 the sum is 1+2+3+4+5+6+7+8+9+(1+0)+(1+1) = 48

Test cases:

N = 110
sum = 957

N = 90
sum = 774

*/

var solution = function (N) {
	var arr = [];
  
  for (var i = 1; i <= N; i += 1) {
  	arr.push(i);
  }  
  
  return arr.reduce(function (acc, d, i) {
  	return acc + +(d).toString().split("").reduce(function (a,b) {
    	return +a + (+b);
    })
  },0);
};

console.log(solution(90));