Click here to Skip to main content
15,885,244 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Objective
If given a month-end in Date format and a frequency,
return a list which includes the given month-end and the 11 preceding month-ends by frequency.

Frequency/duration is month based (E.g. duration of 2 is every 2 months and length species the number of results to return in the list-->
Given
Month-end: '04/30/2022' or given the year and month find the month-end(E.g. Year = 2022 and month = 4 like code below)

Duration: '2'
Length: 3
Expected
Output: ['04/302022', '02/28/2022', '12/31/2021']

const makeDates = (startYear, startMonth, duration = 1, length = 3) =>
    Array.from({ length }, (_, index) =>
        new Date(startYear, startMonth - index * duration, 0)
            .toLocaleDateString("en-US")
            .split("/")
            .map((v) => v.padStart(2, 0))
            .join("/")
    );
    
console.log(makeDates(2022, 4, 2));


What I have tried:

My code is very complex and not working in Python. I'm trying to do the above in python but only have a javascrpit implementation.
Posted
Updated 28-Jul-22 18:53pm
v2
Comments
Richard MacCutchan 29-Jul-22 3:55am    
Show the Python code you have and explain where the problem is. This should be a fairly easy problem to resolve in Python using the Date class.

1 solution

This is not a code conversion service: we are not here to translate code for you.
Even if we did, what you would end up with would not be "good code" in the target language – they are based on very different frameworks, and what makes something work in one language does not always "translate" directly into another.
So what you end up with is very poor code, that is difficult if not impossible to maintain, that can’t be upgraded nicely, and that will cause you immense headaches if the original is changed. And it'll be a nightmare to debug if it doesn’t work "straight out of the box".
Instead, use the source code as a specification for a new app written in and for the target language / framework and write it from scratch using the original as a "template". You will get a much, much better result that will save you a lot of time in the long run.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900