I recently came across this problem and can't find a good answer anywhere (hence the question).
I want to restart the loop once i reach the end yet only loop a finite amount of times.
In this particular context I have an array of days in the week and i want to display the names of days for the next 7 days from today's day of the week using Date.getDay() ,which, returns a value from 0 (sunday) to 6 (saturday). I am able to create an array of the next 7 day names except I keep skipping one because of my current loop. Here's what i've got so far.
My expected output is:
['friday', 'saturday', 'sunday', 'monday', 'tuesday', 'wednesday', 'thursday']
const dayNames = [
"Monday",
"Tuesday",
"Wednesday",
"Thursday",
"Friday",
"Saturday",
"Sunday"
];
const rawDate = new Date();
let dayNum = rawDate.getDay();
const week = [];
for (let i = 0; i < 6; i++) {
if (dayNum + 1 === 7) {
dayNum = 0;
for (j = 0; j < todayNum; j++) {
week.push(dayNames[dayNum])
dayNum++
}
week.push(dayNames[dayNum]);
dayNum++
} else {
week.push(dayNames[dayNum + 1]);
dayNum++;
}
}
console.log(week);
I do see that my "if" in my "for" is the reason one is skipping but i can't seem to get my head around how to fix. Thanks y'all
const rawDate = new Date(2017, 4, 10);...