[英]How to loop through days array starting from the current date
I've built a simple app that prints days from an array.我构建了一个简单的应用程序,可以从数组中打印天数。 When you click the button, you print another day and another etc. However, when I add a starting point - the current day - I cannot loop through following days.当您单击该按钮时,您将打印另一天和另一天等等。但是,当我添加一个起点 - 当天 - 我无法循环播放接下来的几天。
const days = [ "Monday ", "Tuesday ", "Wednesday ", "Thursday ", "Friday ", "Saturday ", "Sunday ", ]; const btn = document.querySelector("#btn"); let d = new Date(); let currentDay = d.getDay(); const list = document.querySelector("#list"); let i = 0; btn.addEventListener("click", function () { let newEl = document.createElement("li"); newEl.textContent = days[currentDay]; // this is the place where I'm stuck // I cannot write: days[currentDay]++ i++; if (i >= days.length) i = 0; list.append(newEl); });
<h2>Days of the week:</h2> <ul id="list"></ul> <button id="btn">Add one more day</button>
You're using days[currentDay]
.您正在使用days[currentDay]
。 But since currentDay
does not change, it won't work但由于currentDay
没有改变,它不会工作
Lets go this way:让 go 这样:
After getting currentDay
, set i
to currentDay
(this is ok since your array has the correct order, we could use indexOf
aswell.获得currentDay
后,将i
设置为currentDay
(这没关系,因为您的数组具有正确的顺序,我们也可以使用indexOf
。
Use i
as index to show the date:使用i
作为索引来显示日期:
const days = [ "Monday ", "Tuesday ", "Wednesday ", "Thursday ", "Friday ", "Saturday ", "Sunday ", ]; const btn = document.querySelector("#btn"); let d = new Date(); let currentDay = d.getDay(); const list = document.querySelector("#list"); let i = currentDay; btn.addEventListener("click", function () { let newEl = document.createElement("li"); newEl.textContent = days[i]; i++; if (i >= days.length) i = 0; list.append(newEl); });
<h2>Days of the week:</h2> <ul id="list"></ul> <button id="btn">Add one more day</button>
The getDay() method returns the day of the week where 0 represents Sunday getDay()方法返回星期几,其中 0 代表星期日
Perhaps all you have to do is make Sunday the first element of the array也许您所要做的就是将星期日设为数组的第一个元素
Please try the following code请尝试以下代码
const days = [
"Sun",
"Mon",
"Tue",
"Wed",
"Thu",
"Fri",
"Sat",
];
var currentDay = new Date().getDay(); // Sun = 0, Mon = 1, ..., Sat = 6
console.log(days[currentDay]);
console.log(days[currentDay + 1]);
Hopefully I did not misunderstand your question希望我没有误解你的问题
Fabio法比奥
The issue was with the placement of ++
that you were trying.问题在于您尝试的++
位置。 Also you don't require an extra variable i
你也不需要额外的变量i
const days = [
"Monday ",
"Tuesday ",
"Wednesday ",
"Thursday ",
"Friday ",
"Saturday ",
"Sunday ",
];
const btn = document.querySelector("#btn");
let d = new Date();
let currentDay = d.getDay();
const list = document.querySelector("#list");
btn.addEventListener("click", function () {
let newEl = document.createElement("li");
newEl.textContent = days[currentDay++];
if (currentDay >= days.length) currentDay = 0;
list.append(newEl);
});
You are incrementing i but you never change the currentDay .你正在增加i但你永远不会改变currentDay 。
In your function, remove i and use currentDay instead.在您的 function 中,删除i并改用currentDay 。
Here is the code:这是代码:
const days = [
"Monday ",
"Tuesday ",
"Wednesday ",
"Thursday ",
"Friday ",
"Saturday ",
"Sunday ",
];
const btn = document.querySelector("#btn");
let d = new Date();
let currentDay = d.getDay();
const list = document.querySelector("#list");
btn.addEventListener("click", function () {
currentDay++; // add 1 to currentDay
if (currentDay > days.length) currentDay = 0; // reset currentDay to 0 each week
let newEl = document.createElement("li");
newEl.textContent = days[currentDay];
list.append(newEl);
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.