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

82
Views
Distribuya los artículos de manera uniforme pero agregue el resto al primero

El fragmento a continuación lo dice todo, pero en resumen, necesito distribuir una cierta cantidad de meses por igual entre las actividades. Dado que siempre existe la posibilidad de tratar con un remanente, estos deben agregarse al primer mes.

 const selectedMonth = 5 const project = { duration: 2, // in months activities: [{ number: 1, title: 'game 1' }, { number: 2, title: 'game 2' }, { number: 3, title: 'game 3' }, ] } // 1 Add a "plannedInMonth" property to each activity // 2 Start planning from the selected month and onwards (the month number can be > 12) // 3 Spread the activities evenly based on the duration function planActivitiesInMonths() {} planActivitiesInMonths() // So this function should, since the remainder of 3 / 2 = 1, return as follows: activities: [{ number: 1, title: 'game 1', plannedInMonth: 5 }, { number: 2, title: 'game 2', plannedInMonth: 5 }, { number: 3, title: 'game 3', plannedInMonth: 6 }, ] // However, it should also work when eg 24 activities need to be distributed across 5 months

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Si solo está buscando copiar y pegar una implementación del algoritmo, esto debería hacerlo:

 function planActivitiesInMonths(project, selectedMonth) { const remainder = project.activities.length % project.duration const activitesPerMonth = Math.floor(project.activities.length / project.duration) return project.activities.map((activity, i) => { let index = Math.floor((i - remainder) / activitesPerMonth) if (index < 0) { index = 0 } activity.plannedInMonth = index + selectedMonth return activity }) }

Solo tenga en cuenta que mi función devuelve un valor y no muta directamente el objeto.

Estoy cambiando el índice por el resto para poder manejar bien el hecho de que las actividades restantes deben agregarse al primer mes, pero hay muchas formas de implementar este algoritmo.


Sin embargo, este algoritmo tiene un comportamiento extraño si la duración del proyecto es ligeramente inferior a un múltiplo de las actividades por mes. En este caso, el resto sería muy grande y se sumarían muchas actividades al primer mes.

Por ejemplo, si desea distribuir 9 actividades en 5 meses, el resto sería 5 % 9 = 4 , por lo que el primer mes tendría un total de 5 actividades.

Tal vez sea mejor distribuir uniformemente el resto también. Y este algoritmo tiene una implementación más limpia y simple:

 function planActivitiesInMonths(project, selectedMonth) { const activitesPerMonth = project.activities.length / project.duration return project.activities.map((activity, i) => { const index = Math.floor(i / project.activities.length * activitesPerMonth) activity.plannedInMonth = index + selectedMonth return activity }) }
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!