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
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
Share Improve this question asked May 10, 2017 at 16:20 rimrafrimraf 4,1377 gold badges32 silver badges56 bronze badges 4-
I would suggest updating your example to use a specific date that will result in your expected output. That way, if someone sees the question tomorrow, they'll still get the expected output. You could just do
const rawDate = new Date(2017, 4, 10);
... – Heretic Monkey Commented May 10, 2017 at 16:25 - You would want to declare a var outside of your loops for tracking the number of forloop's you have pleted. Then in your loops just reset your interator to 0 once it is equal to the final loop interation. When you reset the interator, increment your var tracking the total number of times you have run the for loop. Test for your exit condition (totalForLoops == numberOfTimeYouWantItToRun) and return when its true. – Ken Commented May 10, 2017 at 16:26
- I see what you're saying mike. Also @Nina, That works but it doesn't show the full 7 days... This is my problem. – rimraf Commented May 10, 2017 at 16:27
- WOW. So many answers! I gotta go with Nina cause they were first but you're all right... I need to explore the modulus operator. for sure. – rimraf Commented May 10, 2017 at 16:32
5 Answers
Reset to default 4You could use the remainder operator %
for the right index.
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++) {
week.push(dayNames[(dayNum + i) % dayNames.length]);
}
console.log(week);
You don't need to restart the loop, just use the modulus operator to wrap your array indexes around.
And you need to fix the order of dayNames
so it corresponds with what getDay()
returns. And the loop needs to run 7 times, not just 6 to get the entire week.
const dayNames = [
"Sunday",
"Monday",
"Tuesday",
"Wednesday",
"Thursday",
"Friday",
"Saturday"
];
const rawDate = new Date();
const dayNum = rawDate.getDay();
const week = [];
for (let i = dayNum; i < dayNum + 7; i++) {
week.push(dayNames[i % 7]);
}
console.log(week);
Try using the %
operator and just one loop:
const dayNames = [
"Sunday",
"Monday",
"Tuesday",
"Wednesday",
"Thursday",
"Friday",
"Saturday"
];
const rawDate = new Date();
let dayNum = rawDate.getDay();
const week = [];
for (let i = 0; i < 7; i++) {
week.push(dayNames[dayNum++ % 7]);
}
console.log(week);
Also, the zeroeth day of the week, according to Date.prototype.getDate is Sunday.
If you want [tomorrow, tomorrow + 1, ..., tomorrow + 6]:
for (let i = 1; i <= 7; i++) {
week.push(dayNames[(dayNum + i) % 7]);
}
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);