- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# Product sum difference of digits of a number in JavaScript

## Problem

We are required to write a JavaScript function that takes in a number n. Our function should find the absolute difference between the sum and the product of all the digits of that number.

## Example

Following is the code −

const num = 434312; const sumProductDifference = (num = 1) => { const sum = String(num) .split('') .reduce((acc, val) => acc + +val, 0); const product = String(num) .split('') .reduce((acc, val) => acc * +val, 1); const diff = product - sum; return Math.abs(diff); }; console.log(sumProductDifference(num));

## Output

271

- Related Articles
- Difference between product and sum of digits of a number in JavaScript
- Finding product of Number digits in JavaScript
- Recursive product of all digits of a number - JavaScript
- Prime digits sum of a number in JavaScript
- Largest product of n contiguous digits of a number in JavaScript
- Digit sum upto a number of digits of a number in JavaScript
- Destructively Sum all the digits of a number in JavaScript
- Recursive sum all the digits of a number JavaScript
- Maximum sum and product of the M consecutive digits in a number in C++
- A two-digit number is 4 times the sum of its digits and twice the product of the digits. Find the number.
- A two digit number is 4 times the sum of its digits and twice the product of its digits. Find the number.
- Difference between sum and product of an array in JavaScript
- Recursive product of summed digits JavaScript
- Maximum of sum and product of digits until number is reduced to a single digit in C++
- Checking whether the sum of digits of a number forms a Palindrome Number or not in JavaScript

Advertisements