본문 바로가기
알고리즘/Codewars

[Codewars] Summing a number's digits (7 kyu) / JavaScript

by fluss 2023. 3. 11.

https://www.codewars.com/kata/52f3149496de55aded000410

 

Codewars - Achieve mastery through coding practice and developer mentorship

A coding practice website for all programming levels – Join a community of over 3 million developers and improve your coding skills in over 55 programming languages!

www.codewars.com

 

DESCRIPTION:

Write a function named sumDigits which takes a number as input and returns the sum of the absolute value of each of the number's decimal digits.

For example: (Input --> Output)

10 --> 1
99 --> 18
-32 --> 5

Let's assume that all numbers in the input will be integer values.

 

코드

function sumDigits(number) {
  return Math.abs(number).toString().split('').map(Number).reduce((a, b) => a + b, 0);
}

댓글