Trying to find biggest number of a input (javascript)

I’ve got a problema running now that is: I need a function that find the highest number made by consecutive digits within that number that I received by the parameter.
For example: If my input is 1235789 my output should be 789. If my input is 123689, my output should be 123.

function getbiggestNumber(numberInput) {
    const numberString = numberInput.toString(); // turned into string

    const temporaryResult = []; // create the array of possible solutions which i'd go through to find the highest value inside of it

    for (let i = 0; i < numberString.length; i += 1) {
        const temporary = [numberString[i]]; // create a temporary answer that would serve as a base

        for (let x = i + 1; x < numberString.length; x += 1) {
            const subResult = Number(numberString[i]) - Number(numberString[x]); // the result of the current number minus the following number

            if (subResult === -1) { // if they are in a sequence this should be -1
                temporary.push(numberString[x]); // pushing this number to that temporary answer
            } // here should be some condition for it to keep running, instead getting into another number of for loop
        temporaryResult.push(temporary); //  pushing that temporary answer to the result, so I could keep track of it
    console.log(temporaryResult); //  checking the output

The problem is that this code is only providing double digits inside a array, and that was the only way I found to do this.
I’d be really thankful if someone could give me a light on this.

>Solution :

That looks a bit unnecessarily convoluted. I’d just split the string into chunks based on sequential digits, then call Math.max on all.

const getBiggestNumber = (numberInput) => {
  const digits = [...String(numberInput)].map(Number);
  const chunks = [];
  let lastDigit;
  let chunk = [];
  for (const digit of digits) {
    if (lastDigit === digit - 1) {
      // Continuation of sequence
    } else {
      if (chunk.length) chunks.push(chunk);
      // New sequence:
      chunk = [digit];
    lastDigit = digit;
  return Math.max( => Number(chunk.join('')))

Leave a Reply