- 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
Largest product of n contiguous digits of a number in JavaScript
We are required to write a JavaScript function that takes in two numbers as first and the second argument, let us call them m and n.
The first number will generally be a number with multiple digits and the second number will always be smaller than the number of digits in the first number.
The function should find the group of n consecutive digits from m whose product is the greatest.
For example −
If the input numbers are −
const m = 65467586; const n = 3;
Then the output should be −
const output = 280;
because 7 * 5 * 8 = 280 and it’s the maximum consecutive three-digit product in this number
Example
Following is the code −
const m = 65467586; const n = 3; const largestProductOfContinuousDigits = (m, n) => { const str = String(m); if(n > str.length){ return 0; }; let max = -Infinity; let temp = 1; for(let i = 0; i < n; i++){ temp *= +(str[i]); }; max = temp; for(i = 0; i < str.length - n; i++){ temp = (temp / (+str[i])) * (+str[i + n]); max = Math.max(temp, max); }; return max; } console.log(largestProductOfContinuousDigits(m, n));
Output
Following is the console output −
280
- Related Articles
- Largest product of contiguous digits in Python
- Finding product of Number digits in JavaScript
- Product sum difference of digits of a number in JavaScript
- Recursive product of all digits of a number - JavaScript
- Difference between product and sum of digits of a number in JavaScript
- Find the largest palindrome number made from the product of two n digit numbers in JavaScript
- Find largest number smaller than N with same set of digits in C++
- Find the Largest number with given number of digits and sum of digits in C++
- Recursive product of summed digits JavaScript
- Find largest sum of digits in all divisors of n in C++
- Product of N with its largest odd digit in C
- Smallest number after removing n digits in JavaScript
- Separating digits of a number in JavaScript
- Largest Sum Contiguous Subarray
- Find the difference between the smallest number of 7 digits and the largest number of digits.

Advertisements