w3resource

JavaScript: Sum of the digits of a number

JavaScript Math: Exercise-68 with Solution

Write a JavaScript program to calculate the sum of the digits of a given number.

In mathematics, the digit sum of a natural number in a given number base is the sum of all its digits. For example, the digit sum of the decimal number 6098 would be 6+0+9+8=23.

Sample Data:
6098 -> 23
-501 -> 6
2546 -> 17

Sample Solution-1:

HTML Code:

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>JavaScript function to sum of the digits of a number</title>
</head>
<body>

</body>
</html>

JavaScript Code:

function sum_Of_Digits(n) {
  if (n < 0) n = -n
  let result = 0

  while (n > 0) 
   {
    result += n % 10
    n = Math.floor(n / 10)
  }

  return result
}
console.log(sum_Of_Digits(6098))
console.log(sum_Of_Digits(-501))
console.log(sum_Of_Digits(2546))

Sample Output:

23
6
17

Flowchart:

JavaScript Math flowchart of sum of the digits of a number

Live Demo:

See the Pen javascript-math-exercise-68 by w3resource (@w3resource) on CodePen.


Sample Solution-2:

JavaScript Code:

function sum_Of_Digits(n) {
  if (n < 0) n = -n
   if (n < 10) return n
   return (n % 10) + sum_Of_Digits(Math.floor(n / 10))
}
console.log(sum_Of_Digits(6098))
console.log(sum_Of_Digits(-501))
console.log(sum_Of_Digits(2546))
console.log(sum_Of_Digits(10))
console.log(sum_Of_Digits(5))

Sample Output:

23
6
17
1
5

Flowchart:

JavaScript Math flowchart of sum of the digits of a number

Live Demo:

See the Pen javascript-math-exercise-68-1 by w3resource (@w3resource) on CodePen.


Improve this sample solution and post your code through Disqus

Previous: Sum of a geometric progression.
Next: Find all prime numbers below a given number.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Share this Tutorial / Exercise on : Facebook and Twitter

JavaScript: Tips of the Day

function and arguments

const person = {
  name: 'Lydia Hallie',
  hobbies: ['coding'],
};

function addHobby(hobby, hobbies = person.hobbies) {
  hobbies.push(hobby);
  return hobbies;
}

addHobby('running', []);
addHobby('dancing');
addHobby('baking', person.hobbies);

console.log(person.hobbies);

The addHobby function receives two arguments, hobby and hobbies with the default value of the hobbies array on the person object.
First, we invoke the addHobby function, and pass "running" as the value for hobby and an empty array as the value for hobbies. Since we pass an empty array as the value for y, "running" gets added to this empty array.
Then, we invoke the addHobby function, and pass "dancing" as the value for hobby. We didn't pass a value for hobbies, so it gets the default value, the hobbies property on the person object. We push the hobby dancing to the person.hobbies array.
Last, we invoke the addHobby function, and pass "bdaking" as the value for hobby, and the person.hobbies array as the value for hobbies. We push the hobby baking to the person.hobbies array.
After pushing dancing and baking, the value of person.hobbies is ["coding", "dancing", "baking"]

Ref: https://bit.ly/2Hcpkm6