
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- 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 and Sum Difference of Digits 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
Advertisements