Display Text "Even Day" Or "Odd Day" Based On GetDay Object JavaScript
Solution 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...!
Solution 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>Solution 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>
Solution 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";
}
Solution 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>
Post a Comment for "Display Text "Even Day" Or "Odd Day" Based On GetDay Object JavaScript"