Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

329
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda