Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

333
Vistas
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. Thanks!

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

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
      chunk.push(digit);
    } else {
      if (chunk.length) chunks.push(chunk);
      // New sequence:
      chunk = [digit];
    }
    lastDigit = digit;
  }
  chunks.push(chunk);
  return Math.max(
    ...chunks.map(chunk => Number(chunk.join('')))
  );
};
console.log(getBiggestNumber(1235789));

about 4 years ago · Juan Pablo Isaza Denunciar

0

Another approach:

const largestStreak = (input) => Math .max (... [...String (input)] .map (Number) .reduce (
  (a, d, i, xs) =>
    d ==  a .at (-1) .at (-1) + 1
      ? [... a .slice (0, -1), [... a .at (-1), d]]
      : [... a, [d]],
  [[]]
) .map (ns => Number (ns .join (''))))

console .log (largestStreak (1235789))
console .log (largestStreak (1235689))

This goes through the following steps:

input:

1235789

[...String (input)] .map (Number):

[1, 2, 3, 4, 7, 8, 9]

reduce accumulator, step-by-step:

[[]]
[[], [1]]
[[], [1, 2]]
[[], [1, 2, 3]]
[[], [1, 2, 3], [5]]
[[], [1, 2, 3], [5], [7]]
[[], [1, 2, 3], [5], [7, 8]]
[[], [1, 2, 3], [5], [7, 8, 9]]

.map (ns => Number (ns .join (''))):

[0, 123, 5, 789]

Math .max (...):

789
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda