
- 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
Difference between product and sum of digits of a number in JavaScript
We are required to write a JavaScript function that takes in an positive integer as the only argument.
The function should first count the sum of the digits of the number and then their product. Finally, the function should return the absolute difference of the product and the sum.
For example −
If the input number is −
const num = 12345;
Then the output should be −
const output = 105;
Example
Following is the code −
const num = 12345; const product = (num, res = 1) => { if(num){ return product(Math.floor(num / 10), res * (num % 10)); } return res; }; const sum = (num, res = 0) => { if(num){ return sum(Math.floor(num / 10), res + (num % 10)); } return res; }; const productSumDifference = (num = 1) => { return Math.abs(product(num) - sum(num)); }; console.log(productSumDifference(num));
Output
Following is the console output −
105
- Related Articles
- Product sum difference of digits of a number in JavaScript
- Difference between sum and product of an array 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
- Maximum sum and product of the M consecutive digits in a number in C++
- Digit sum upto a number of digits of a number in JavaScript
- Absolute difference between sum and product of roots of a quartic equation?
- Destructively Sum all the digits of a number in JavaScript
- Recursive sum all the digits of a number JavaScript
- 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.
- Maximum of sum and product of digits until number is reduced to a single digit in C++
- Difference between sum of square and square of sum in JavaScript

Advertisements