Newbie here! I want to start the loop based on today (ex:"Thursday") and I want to make it stop on "Wednesday"...is that possible? Any help is appreciate! thanks
let days = [
"Sunday",
"Monday",
"Tuesday",
"Wednesday",
"Thursday",
"Friday",
"Saturday",
];
let date = new Date();
let today = date.getDay();
let result = [];
for (let i = today; i < days.length; i ) {
result.push(days[i]);
}
return result[index];
};```
CodePudding user response:
For simplicity, you could loop from today
to days.length
, then again from 0
to today
.
let days = [
"Sunday",
"Monday",
"Tuesday",
"Wednesday",
"Thursday",
"Friday",
"Saturday",
];
let date = new Date();
let today = date.getDay();
let result = [];
for (let i = today; i < days.length; i ) {
result.push(days[i]);
}
for (let i = 0; i < today; i ) {
result.push(days[i]);
}
console.log(result);