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 a
You need i < 7
to get the entire next 7 days, then using the modulus operator %
which returns the remainder of division and a single for loop will give you the desired result.
const dayNames = [
"Monday",
"Tuesday",
"Wednesday",
"Thursday",
"Friday",
"Saturday",
"Sunday"
];
const rawDate = new Date();
let dayNum = rawDate.getDay();
const week = [];
for (let i = 0; i < 7; i++) {
week.push(dayNames[(dayNum + i) % dayNames.length]);
}
console.log(week);