how to create a function that takes ANY number and finds the LARGEST number formed by consecutive digits in that number in Javascript ?
example:
input: 90123609789
output: 90123
input: 53590 output 90
input:674030098567819 output: 5678
>Solution :
You can try all substrings where the difference between adjacent digits is 1 under modulus 10.
function largestConsecutiveDigits(n) {
let str = String(n),
res = Math.max(...str);
for (let i = 0; i < str.length; i++) {
for (let j = i + 1; j < str.length; j++) {
if ((str[j] - str[j - 1] + 10) % 10 === 1)
res = Math.max(res, str.slice(i, j + 1));
else break;
}
}
return res;
}
console.log(largestConsecutiveDigits('90123609789'));
console.log(largestConsecutiveDigits('53590'));