skip to Main Content

When I remove the getday() method, the string is displayed, but when I add the method, even the string is not displayed.
The same thing happens in Firefox
Antivirus is disabled

Code :

<html>
<body>
    <script type="text/javascript">
        d = new Date();
        switch(d.getday()){
            case 0:
                day = "Sunday";
                break
            case 1:
                day = "Monday"
                break
            case 2:
                day = "Tuesday"
                break
            case 3:
                day = "Wednesday"
                break
            case 4:
                day = "Thursday"
                break
            case 5:
                day = "Friday"
                break
            case 6:
                day = "Saturday"
                break
        document.write("<b> today is: ", day);                                                   
        }
    </script>
</body>

Result :
enter image description here

I searched about this problem, but only one person had the same problem because he/she copied the codes from somewhere and the problem was solved by writing the code. I did not copy the code, so… please help me.

2

Answers


  1. Chosen as BEST ANSWER

    I did not follow the camelcase writing rules in calling the getday() method getDay() is true.


  2. there were a few errors that I found in the code you provided.

    1. you are using d = new Date();, but if you declare a variable, you’ll have to use keywords like const, let, or var. Same with the day variable.
    2. the document.write() statement is inside the switch statement.

    after resolving these two errors, here is the updated version of the code:

    <script>
      const d = new Date();
      let day;
      switch (d.getDay()) {
        case 0:
          day = "Sunday";
          break;
        case 1:
          day = "Monday";
          break;
        case 2:
          day = "Tuesday";
          break;
        case 3:
          day = "Wednesday";
          break;
        case 4:
          day = "Thursday";
          break;
        case 5:
          day = "Friday";
          break;
        case 6:
          day = "Saturday";
          break;
        default:
          break;
      }
    
      document.write(`<b> today is: ${day}`);
    </script>
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search