我正在尝试显示文本“偶数日”,当月份的日期是= 2,4,6 ......和“奇数日”当= 1,3,5等我试过显示通过连接到getDay对象的数组的文本,但它似乎没有输出任何东西。
感谢所有帮助!
进一步实施:
好吧,我还有另外一个问题。我怎样每隔一天发出一次“A Day”或“B Day”的文字,无论日期是偶数还是奇数?
这是我的代码:
<html>
<body>
<h2>What day is it?</h2>
<p id="demo"></p>
<script>
function myFunction() {
var time = new Date().getDay();
var odd = ["1", "3",
"5","7","9","11","13","15","17","19","21","23","25","27","29","31"];
var even = ["2","4",
"6","8","10","12","14","16","18","20","22","24","26","28","30"];
if (time = odd) {
greeting = "Odd Day";
} else if (time = even) {
greeting = "Even Day";
document.getElementById("demo").innerHTML = greeting;
</script>
<script type="text/javascript">
document.write(myFunction())
</script>
</body>
</html>
答案 0 :(得分:2)
实际上你的代码很好,只需关闭大括号即可。并检查数字是否在数组中使用array.includes(value)
它将正常工作
<html>
<body>
<h2>What day is it?</h2>
<p id="demo"></p>
<script>
function myFunction() {
var time = new Date().getDay();
var odd = ["1", "3",
"5","7","9","11","13","15","17","19","21","23","25","27","29","31"];
var even = ["2","4",
"6","8","10","12","14","16","18","20","22","24","26","28","30"];
checknum = odd.includes(time);
if (checknum == true) {
greeting = "Odd Day";
} else {
greeting = "Even Day";
}
//document.getElementById("demo").innerHTML = greeting;
console.log(time);
console.log(checknum);
console.log(greeting);
}
</script>
<script type="text/javascript">
document.write(myFunction())
</script>
</body>
</html>
现在它可以正常工作。
希望这有助于......!
答案 1 :(得分:1)
为了测试当前日期是偶数还是奇数,您只需测试:
//N is input (N<=4*10^16), let's find n by Binet's formula
double a = log10(sqrt (5) * N);
double b = log10((1 + sqrt (5)) / 2);
int n = (int) (a / b);
//Using same formula, we find every Fibonacci number till n
for (int i = 1; i <= n; i++){
a = (1 + sqrt(5)) / 2;
long fiboNum = Math.round (Math.pow(a,i) / sqrt(5));
此外,您需要使用:
getDate():根据当地时间返回指定日期的月中日期。
time % 2 != 0 --> ODD
&#13;
function myFunction() {
var time = new Date().getDate();
if (time % 2 != 0) {
greeting = "Odd Day";
} else {
greeting = "Even Day";
}
document.getElementById("demo").innerHTML = greeting;
}
myFunction();
&#13;
答案 2 :(得分:0)
在窗口加载时调用该函数并使用日期值的索引来检查它是在奇数数组还是在偶数数组中
<html>
<head>
</head>
<body>
<h2>What day is it?</h2>
<p id="demo"></p>
<script type="text/javascript">
function myFunction() {
var time = new Date();
var day= time.getDay();
var odd = ["1", "3",
"5","7","9","11","13","15","17","19","21","23","25","27","29","31"];
var even = ["2","4",
"6","8","10","12","14","16","18","20","22","24","26","28","30"];
if (odd.indexOf(day)>-1) {
greeting = "Odd Day";
}
else {
greeting = "Even Day";
document.getElementById("demo").innerHTML = greeting;
}
}
</script>
<script type="text/javascript">
window.onload= myFunction();
</script>
</body>
</html>
答案 3 :(得分:0)
正如@DavidG所指出的那样,你对表的使用是错误的。
您需要使用for循环并依次将日期日值与表格中的每个元素进行比较。
为什么使用查找表?检查均匀度的正确方法是:
NSMetadataQuery
答案 4 :(得分:0)
您的功能未被}
关闭,而您的else
声明也未被}
关闭。 getDay()
返回星期几。您需要.getDate()
来返回该月的某一天。
我删除了你的数组,而是通过使用模数% 2
检查日期数是否为偶数,模数0
将日数除以2并返回余数。因此,如果余数为<h2>What day is it?</h2>
<p id="demo"></p>
<script type="text/javascript">
myFunction();
function myFunction() {
var time = new Date().getDate();
if (time % 2 == 0) {
greeting = "Even Day";
} else {
greeting = "Odd Day";
}
document.getElementById("demo").innerHTML = greeting;
}
</script>
,则天数为偶数。
--reset-offsets Reset offsets of consumer group.
Supports one consumer group at the
time, and instances should be
inactive
Has 3 execution options: (default) to
plan which offsets to reset, --
execute to execute the reset-offsets
process, and --export to export the
results to a CSV format.
Has the following scenarios to choose:
--to-datetime, --by-period, --to-
earliest, --to-latest, --shift-by, --
from-file, --to-current. One
scenario must be choose
To define the scope use: --all-topics
or --topic. . One scope must be
choose, unless you use '--from-file'
scenario