
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
Summing up all the digits of a number until the sum is one digit in JavaScript
We are required to create a function that takes in a number and finds the sum of its digits recursively until the sum is a one-digit number.
For example
findSum(12345) = 1+2+3+4+5 = 15 = 1+5 = 6
Therefore, the output should be 6.
Let’s write the code for this function findSum()
// using recursion const findSum = (num) => { if(num < 10){ return num; } const lastDigit = num % 10; const remainingNum = Math.floor(num / 10); return findSum(lastDigit + findSum(remainingNum)); } console.log(findSum(2568));
We check if the number is less than 10, it’s already minified and we should return it and from the function otherwise we should return the call to the function that recursively takes the last digit from the number adds to it until it becomes less than 10.
Therefore, the output for this code will be −
3
- Related Articles
- Sum up a number until it becomes one digit - JavaScript
- Sum up a number until it becomes 1 digit JavaScript
- Program to find sum of digits until it is one digit number in Python
- Finding sum of digits of a number until sum becomes single digit in C++
- C++ program to find sum of digits of a number until sum becomes single digit
- Reduce sum of digits recursively down to a one-digit number JavaScript
- Maximum of sum and product of digits until number is reduced to a single digit in C++
- Digit sum upto a number of digits of a number in JavaScript
- Recursive sum all the digits of a number JavaScript
- Destructively Sum all the digits of a number in JavaScript
- Summing up digits and finding nearest prime in JavaScript
- Find the smallest sum of all indices of unique number pairs summing to a given number in JavaScript
- The sum of digits of a two-digit number is 13. If the number is subtracted from the one obtained by interchanging the digits, the result is 45. What is the number?
- The units digit of a two digit number is 3 and seven times the sum of the digits is the number itself. Find the number.
- A two-digit number is 4 times the sum of its digits and twice the product of the digits. Find the number.

Advertisements