JavaScript Program for Sum of Digits of a Number
Last Updated :
28 May, 2024
In this article, we are going to learn about finding the Sum of Digits of a number using JavaScript. The Sum of Digits refers to the result obtained by adding up all the individual numerical digits within a given integer. It’s a basic arithmetic operation. This process is repeated until a single-digit sum, also known as the digital root.
There are several methods that can be used to find the Sum Of Digits by using JavaScript, which are listed below:
We will explore all the above methods along with their basic implementation with the help of examples.
Approach 1: Using Array Reduce() Method
In this approach, the reduce method transforms each digit of a number into an accumulated sum. It converts the number to a string, iterates, and adds each digit to the sum.
Syntax:
array.reduce( function(total, currentValue, currentIndex, arr), initialValue )
Example: In this example, the sumOfDigit function converts the number to a string, splits it into digits, and then reduces by adding parsed digits, resulting in the sum.
JavaScript
function sumOfDigit(num) {
return num.toString().split("")
.reduce((sum, digit) =>
sum + parseInt(digit), 0);
}
console.log(sumOfDigit(738));
In this approach, Iterate through each digit of a number by converting it to a string, then use a for…of loop to add parsed digits, resulting in the sum.
Syntax:
for ( variable of iterableObjectName) {
. . .
}
Example: In this example we are using the above-explained approach.
JavaScript
function sumOfDigit(num) {
let numStr = num.toString();
let sum = 0;
for (let digit of numStr) {
sum += parseInt(digit);
}
return sum;
}
console.log(sumOfDigit(738));
Approach 3: Using Math.floor and Division
In this approach, we calculate sum by repeatedly adding last digit using remainder of 10 and updating number by division, until it’s 0.
Syntax:
Math.floor( value )
Example: In this example we are using above-explained approach.
JavaScript
function sumOfDigits(num) {
let sum = 0;
for (; num > 0; num = Math.floor(num / 10)) {
sum += num % 10;
}
return sum;
}
console.log(sumOfDigits(456));
Approach 4: Using forEach
In this approach, we are converting the number to a string, split it into digits, and use forEach loop to add parsed digits, obtaining the sum.
Syntax:
array.forEach(callback(element, index, arr), thisValue)
Example: In this example, the number 123 is converted to a string and then split into an array of individual digits. The forEach loop iterates through each digit in the array. Inside the loop, each digit is parsed and added to the sum variable.
JavaScript
function sumOfDigit(num) {
let sum = 0;
num.toString().split("").forEach(digit => {
sum += parseInt(digit);
});
return sum;
}
console.log(sumOfDigit(123));
Approach 5: Using Recursion
In this approach, we use a recursive function to repeatedly add the last digit of the number (obtained using modulo 10) and call itself with the number divided by 10 until the number becomes 0.
Example: In this example, the recursiveSum function calls itself with the number divided by 10, adding the last digit each time, until the number becomes 0.
JavaScript
function recursiveSum(num) {
if (num === 0) {
return 0;
}
return (num % 10) + recursiveSum(Math.floor(num / 10));
}
console.log(recursiveSum(738));
Similar Reads
JavaScript Program for Sum of Digits of a Number using Recursion
We are given a number as input and we have to find the sum of all the digits contained by it. We will split the digits of the number and add them together using recursion in JavaScript. Table of Content Recursively Summing DigitsUsing string manipulation with recursionRecursively Summing DigitsIn th
2 min read
JavaScript Program for Sum of Number Digits in a Linked List
We are going to write a JavaScript program on how to find the sum of number digits stored in a linked list. We are going to use the Iterative and Recursive techniques to find the Sum of number digits. A linked list is a data structure where elements are stored in nodes and each node points to the ne
2 min read
JavaScript Program to Find Sum of Even Numbers of an Array
In JavaScript, working with arrays is a basic operation. We have to sum all the even numbers present in the array. We can check the number if it is even or not by the use of the % operator. These are the following ways to find the sum of Even numbers in an Array: Table of Content Iterative ApproachU
4 min read
JavaScript Program to Find the Sum of Digits in a Factorial
Let's first define the problem before proceeding to a solution. Any positive number less than or equal to n is the product of all positive integers, and the factorial of a non-negative integer n is denoted by n!. For example, 5!, sometimes known as "five factorial," is equivalent to 5 Ã 4 Ã 3 Ã 2 Ã
3 min read
JavaScript Program to Construct Largest Number from Digits
In this article, we have given a set of digits, and our task is to construct the largest number generated through the combination of these digits. Below is an example for a better understanding of the problem statement. Example: Input: arr[] = {4, 9, 2, 5, 0}Output: Largest Number: 95420Table of Con
3 min read
JavaScript Program to Check if Two Numbers have Same Last Digit
In this article, we will discuss how to check if two numbers have the same last digit in JavaScript. Checking the last digit of a number is a common requirement in programming tasks, and it can be useful in various scenarios. We will explore an approach using JavaScript to accomplish this task. Meth
4 min read
JavaScript Program to Add n Binary Strings
In this article, we are going to learn about Adding n binary strings by using JavaScript. Adding n binary strings in JavaScript refers to the process of performing binary addition on a collection of n binary strings, treating them as binary numbers, and producing the sum in binary representation as
3 min read
JavaScript Program to Multiply the Given Number by 2 such that it is Divisible by 10
In this article, we are going to implement a program through which we can find the minimum number of operations needed to make a number divisible by 10. We have to multiply it by 2 so that the resulting number will be divisible by 10. Our task is to calculate the minimum number of operations needed
3 min read
JavaScript Program for Decimal to any base conversion
In this JavaScript article, we will see how we can do decimal to any base conversion in JavaScript. The base can not be less than 2 and can not exceed 36, So we always have to find out the base of a decimal that lies in between this range, which is '2=< base <=36'. Example: Input: number = "11
5 min read
JavaScript Program to Add Two Binary Strings
Here are the various ways to add two binary strings Using parseInt() and toString() The parseInt() method used here first converts the strings into the decimal. Ten of these converted decimal values are added together and by using the toString() method, we convert the sum back to the desired binary
4 min read