显示文字“ Even Day”或“奇数日”基于getDay对象的JavaScript [英] Display text "Even Day" or "Odd Day" based on getDay object JavaScript
问题描述
我试图在当月的一天== 2,4,6 ...时显示文本 Even Day,而在= 1,3,5时显示文本 Odd Day,等等。通过连接到getDay对象的数组获取文本,但似乎未输出任何内容。
I am trying to display the text "Even Day" when the day of the month is = to 2,4,6... and "Odd Day" when = 1,3,5, etc. I have tried displaying the text through an array that is connected to the getDay object but it doesn't seem to be outputting anything.
感谢所有帮助!
进一步实现:
好吧,我还有另一个问题从此分支出来。
Further implementation:
Alright, i have another question branching off from this. How can i emit the text "A Day" or "B Day" every other day, regardless of the date being even or odd?
这是我的代码:我如何隔两天发出文本 A Day或 B Day? p>
Here is my code:
<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>
推荐答案
实际上,只需将大括号括起来即可。并检查数字是否在数组中,请使用 array.includes(value)
可以正常工作
Actually Your code is fine just close the curly brackets. and to check if the number is in array use array.includes(value)
It will work fine
<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>
现在它将正常工作。
希望这可以帮助...!
Hope this helps...!
这篇关于显示文字“ Even Day”或“奇数日”基于getDay对象的JavaScript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!