Display text “Even Day” or “Odd Day” based on getDay object JavaScript

南笙酒味 提交于 2020-07-24 02:59:25

问题


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.

All help is appreciated!

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?

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>

回答1:


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>

Now it will work fine.

Hope this helps...!




回答2:


In order to test if the current date is even or odd you can simply test:

time % 2 != 0  --> ODD

Moreover you need to use:

getDate(): returns the day of the month for the specified date according to local time.

function myFunction() {
    var time = new Date().getDate();

    if (time % 2 != 0) {
        greeting = "Odd Day";
    } else  {
        greeting = "Even Day";
    }
    document.getElementById("demo").innerHTML = greeting;
}

myFunction();
<h2>What day is it?</h2>

<p id="demo"></p>



回答3:


call the function on window load and use of index of for the day value to check if it is in odd array or in even array

<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>



回答4:


Your use of tables is wrong, as pointed out by @DavidG.

You'd need to use a for loop and compare your date-day value with each element of the table in turn.

Why are you using look-up tables? The correct way to check for evenness is:

var day = new Date().getDay();
if ((day & 1) == 0)
{
   greetings = "even";
}
else
{
   greetings = "odd";
}



回答5:


Your fucntion was not being closed with a }, and your else statement was also not being closed by a }. getDay() returns the day of the week. You need .getDate() which returns the day of the month.

I removed your array and instead checked if the day number was even by using modulus % 2, which divides the day number by 2 and returns the remainder. So if the remainder is 0 the day number is even.

<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>


来源:https://stackoverflow.com/questions/44738685/display-text-even-day-or-odd-day-based-on-getday-object-javascript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!