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

234
Visualizações
for loop function called twice, for loop only occurs in second call

I have the following helper function I created, which is to serve to create an array of a date range. Assume that the holidays variable is an array of ISO date strings and is already populated elsewhere in my vue app.

export const parseAvailableDates = (
    startDate,
    endDate,
    holidays
) => {
    console.log({ startDate, endDate, holidays });
    let dates = [];
    for (
        let i = new Date(startDate);
        i <= new Date(endDate);
        i.setDate(i.getDate() + 1)
    ) {
        console.log("running");
        const thisDate = i.toLocaleString("en-US", localeString);
        if (!thisDate.includes("Sunday") && !thisDate.includes("Saturday")) {
            const formattedDate = changeLocaleDateToISOString(thisDate);
            if (!holidays.includes(formattedDate)) {
                dates.push(formattedDate);
            }
        }
    }
    console.log(dates);
    return dates;
};

Now, I need to call this function twice in my Vue app in the same function, once to initially populate the dates based on the range between startAssignments and lastDateToSubmitWork, and again to reset the availableDates array to the range between startAssignments and endDate.

// called here

    const startAssignments = `2022-02-09T00:00:00.000+04:00`
    const lastDateToSubmitWork = `2022-08-19T00:00:00.000+04:00`
    const endDate = `2022-08-31T00:00:00.000+04:00`

      let availableDates = [];
      availableDates = parseAvailableDates(
        startAssignments,
        lastDateToSubmitWork,
        holidays
      );
      console.log(availableDates);

// also called here

          availableDates = parseAvailableDates(
            startAssignments,
            endDate,
            holidays
          );

When these two calls happens, the initial console.log() in parseAvailableDates() shows the startDate, endDate, and holidays properly defined on both calls. However, on the first call, the for loop never runs, and the function returns [], and on the second call, the for loop does run, and the function returns a properly populated array of ISO date strings.

I don't understand why the first loop doesn't run properly, but the second does.

about 4 years ago · Juan Pablo Isaza
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