Answers for "sum of digits with reduce function"

15

sum of number using reduce

console.log(
  [1, 2, 3, 4].reduce((a, b) => a + b, 0)
)
console.log(
  [].reduce((a, b) => a + b, 0)
)
Posted by: Guest on January-22-2020
1

sum of digits with reduce function

import functools
from functools import reduce


def sum_of_digits(number):
    return functools.reduce(add, to_list(number))


def add(x, y):
    return x + y


def to_list(num):
    return list(map(int, str(num)))
Posted by: Guest on April-27-2022

Code answers related to "TypeScript"

Browse Popular Code Answers by Language