Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

327
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!