Finding the largest 5 digit number within the input number using JavaScript


Problem

We are required to write a JavaScript function that takes in a string number of at least five digits. Our function should return the greatest sequence of five consecutive digits found within the number given.

Example

Following is the code −

 Live Demo

const num = '123546544';
const findGreatestFiveDigit = (num = '') => {
   const str = num.toString();
   const arr = [];
   for(let i = 0; i < str.length; i++){
      arr.push(str.slice(i, i + 5));
   };
   return Math.max(...arr);
};
console.log(findGreatestFiveDigit(num));

Output

54654

Updated on: 19-Apr-2021

318 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements