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

206
Views
How to make recursive function to work in procedural order in Node.js?

I am trying to make a calendar array in Node.js. this is my main.mts:

import dayjs from 'dayjs';
import customParseFormat from 'dayjs/plugin/customParseFormat';

dayjs.extend(customParseFormat);

function nextDay(d: string) {
    const e = dayjs(d, 'MMM D, YYYY (ddd)');
    const f = dayjs(e).add(1, 'day');
    return dayjs(f).format('MMM D, YYYY (ddd)');
}

export class CalendarEdit {
    private cal: string[] = [];
    constructor(d: string) {
        this.cal.push(d);
        this.init(365).catch(err => { throw err; });
    }

    public addDate() {
        const d = this.cal[this.cal.length - 1];
        const e = nextDay(d);
        this.cal.push(e);
    }

    private init(j: number) {
        this.addDate();
        if (j > 1)
            this.init(j - 1);
    }

    public printCal() {
        console.log(this.cal);
    }
}

but result says every element in cal array is same, which has to be last day of calendar.
I know that JavaScript is basically non-blocking language, but I cannot find a way.
I tried callbacks and promises, and async, await pairs. but none of them worked.

I googled it but I cannot find a solution does work on my case. anyone knows the answer?

EDIT: I call CalendarEdit in my index.mts like this:

import { CalendarEdit } from './main';

let cal = new CalendarEdit('Jun 1, 2022 (Wed)');
cal.printCal();
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

I edited my addDate() to this and it solved error:

public addDate() {
    const d = nextDay(this.cal[this.cal.length - 1]);
    this.cal.push(d);
}
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!