🧮

Sum of Digits

Sum of Digits
쉬움 알고리즘 +10pts

Problem

Write a function that returns the sum of all digits in a number.

Examples

Input: sumDigits(123)
Output: 6
💡 1 + 2 + 3 = 6

Explanation

이 문제는 **문자열 변환과 reduce**를 조합하여 자릿수 합을 구하는 방법을 학습합니다. ## 핵심 개념: 자릿수 분리 숫자를 문자열로 변환하여 각 자릿수에 접근합니다. ### 접근 방법 ```javascript Math.abs(n).toString().split('').reduce((sum, d) => sum + Number(d), 0) ``` ### 단계별 분석 1. Math.abs(n): 음수 처리 2. toString(): 숫자 → 문자열 3. split(''): 각 자릿수 분리 4. reduce: 합계 계산 ...

View detailed explanation →

Key Concepts

문자열 변환 reduce 자릿수 합 Math.abs
Time: O(log n) Space: O(log n)
solution.js
Ctrl + Enter
Run tests to see results here.