logo CodeStepByStep logo

digitSum

Language/Type: JavaScript recursion return
Author: Eric Roberts (added by Melissa Hovik on 2017/08/14)

Write a recursive function named digitSum that accepts an integer as a parameter and returns the sum of its digits. For example, calling digitSum(1729) should return 1 + 7 + 2 + 9, which is 19. If the number is negative, return the negation of the value. For example, calling digitSum(-1729) should return -19.

Constraints:

  • Do not use any loops; you must use recursion.
  • Do not declare any global variables.
  • Do not use arrays.
  • You may define other "helper" functions if you like; they are subject to these same constraints.

Function: Write a JavaScript function as described, not a complete program.

You must log in before you can solve this problem.

Log In

Need help?

Stuck on an exercise? Contact your TA or instructor.

If something seems wrong with our site, please

Is there a problem? Contact us.