I'm writing this in JS but anything similar in Java or C++ would work too! So essentially I need to gather values from multiple arrays based on a given number of iterations. The best way I can describe this is like a calendar. I similarly have an array for days, weeks, months, and years:
const days = ["S", "M", "T", "W", "T", "F", "S"];
const weeks = ["Week 1", "Week 2", "Week 3", "Week 4"];
const months = ["Jan", "Feb", "Mar", "Apr", "May"...];
const years = ["2000", "2001", "2002", "2003", "2004"...];
The idea is to find the date from given number of total days. For example, 30 days (assuming there are exactly 4 weeks in a month in this application) would loop through the days array 30 times, through the weeks 4 times, and through the month one time to gather the array vales as "M, Week 1, Feb, 2000". The part I am struggling with is I have nested loops, but once I have iterated through the 30 days, I need the loops to stop where they are and print/log the values they are currently on.
while (totalDays > 0) {
for (let i = 0; i < totalDays < 7; i++) {
day = days[i];
totalDays -= 1;
if (totalDays == 0) {
break;
}
for (let i = 0; i < totalDays / 7; i++) {
week = weeks[i];
totalDays = -1;
if (totalDays == 0) {
break;
}
for (let i = 0; i < totalDays / 12; i++) {
week = weeks[i];
totalDays = -1;
if (totalDays == 0) {
break;
}
for (let i = 0; i < totalDays / 365; i++) {
week = weeks[i];
totalDays = -1;
if (totalDays == 0) {
break;
}
}
}
}
}
}
So how do I make this loop like a big clock or calendar that will loop through and stop at a given number of increments and output the exact times/values in each array?
Any help is much appreciated! Thanks.