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

190
Visualizações
JS function that creates a range of numbers (inclusive beginning and end) with a limit on range items

You are given a starting number and ending number and the max number of output elements allowed. How would you create an output array with as even a distribution as possible, while still including the first and last points in the output?

Function signature

function generatePoints(startingNumber, endingNumber, maxPoints) {}

Function desired output

generatePoints(0, 8, 5) // [0, 2, 4, 6, 8]

Here's what I tried so far
function generatePoints(startingNumber, endingNumber, maxPoints) {
   const interval = Math.round((endingNumber - startingNumber) / maxPoints)
    let count = 0
    let counter = 0
    let points = []
   
    while(count < maxPoints - 1) {
        points.push(counter)
        counter+=interval
        count++
    }

    points.push(endingNumber)

    return points
}

Technically this creates the correct output for the simple case, but falls short when up against most other edge cases due to the fact that I'm stopping one iteration early and then adding the final point. I'm thinking that the better way to do this (to create a better distribution) is to build from the center of the array outwards, versus building from the start of the array and then stopping one element early and appending the endingNumber.

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

Note this:

  0       2       4       6        8
   +-----+ +-----+ +-----+ +-----+
      A       B       C       D

Splitting our range into intervals with 5 points including the endpoints, we have only four intervals. It will always be one fewer than the number of points. We can divide our range up evenly into these smaller ranges, simply by continually adding the width of one interval, which is just (endingNumber - startingNumber) / (maxPoints - 1). We can do it like this:

const generatePoints = (startingNumber, endingNumber, maxPoints) => Array .from (
  {length: maxPoints}, 
  (_, i) => startingNumber + i * (endingNumber - startingNumber) / (maxPoints - 1)
) 

console .log (generatePoints (0, 8, 5))

We just build an array of the right length, using the index parameter to count the number of smaller intervals we're using.

We do no error-checking here, and if maxPoints were just 1, we might have an issue. But that's easy enough to handle how you like.


But there is a concern here. Why is the parameter called maxPoints instead of points? If the number of points allowed is variable, I think we need further requirements.

about 4 years ago · Juan Pablo Isaza Relatório

0

Do not Math.round(interval). Instead Math.round(counter) at that last moment.

The reason why is that if you've added k intervals, the error in what you're going can be as much as 0.5*k. But if you round at the last minute, the error is never more than 0.5.

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