Skip to content Skip to sidebar Skip to footer

Display Text "even Day" Or "odd Day" Based On Getday Object Javascript

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 con

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><pid="demo"></p><script>functionmyFunction() {
    var time = newDate().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><scripttype="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.

functionmyFunction() {
    var time = newDate().getDate();

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

myFunction();
<h2>What day is it?</h2><pid="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><pid="demo"></p><scripttype="text/javascript">functionmyFunction() {
        var time = newDate();
        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><scripttype="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 = newDate().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><pid="demo"></p><scripttype="text/javascript">myFunction();
    
    functionmyFunction() {
      var time = newDate().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"