Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save qodirovshohijahon/02916e314cd18f49729c50d673c7d367 to your computer and use it in GitHub Desktop.
Save qodirovshohijahon/02916e314cd18f49729c50d673c7d367 to your computer and use it in GitHub Desktop.
Subtract the Product and Sum of Digits of an Integer
**
* @param {number} n
* @return {number}
*/
var subtractProductAndSum = function(n) {
let num = n.toString().split("");
let count = num.reduce((acc, sum)=>{
acc += Number(sum);
return acc;
}, 0);
let mul = num.reduce((acc, sum)=>{
acc *= Number(sum);
return acc;
}, 1);
return mul - count;
};
@qodirovshohijahon
Copy link
Author

Given an integer number n, return the difference between the product of its digits and the sum of its digits.

Example 1:

Input: n = 234
Output: 15
Explanation:
Product of digits = 2 * 3 * 4 = 24
Sum of digits = 2 + 3 + 4 = 9
Result = 24 - 9 = 15

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment