Skip to content
Exercise · Conditions and loops · lesson 11 of 13

Exercise: digit sum

Write a function function digitSum(n) that returns the sum of the decimal digits of the number n.

  • digitSum(123) → 6
  • digitSum(9045) → 18
  • digitSum(0) → 0

Constraints: n is an integer from 0 to 1000000000; the number 0 has one digit, and the sum of its digits is 0.

Do not add extra imports unless the task says so explicitly.

digits.js — javascript
javascript · 4 lines
Ctrl+Enter run · Esc leave editor

Subscribe to take the course

The course is free. Sign in with Telegram and subscribe to the channel to open every lesson.

Sign in another way