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

233
Views
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
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!