Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 10-01-2011, 07:30 PM   PM User | #1
lucromick
New to the CF scene

 
Join Date: Aug 2011
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
lucromick is an unknown quantity at this point
code for message of the day

Hello all,

I'm trying to write a simple script in JS that will show a message for each day of the week. I have searched in your forums and other places but sadly I keep failing.

Thank you for helping a newb.

Code:
<script type="JavaScript">


function showMessage()
{
    var d = new Date();
    var day = d.getDay();     // Note: Sunday=0, Monday=1, etc...

    switch( true )
   
        }
        case ( day == 6 ) :
        {
          document.write("Today is saturday!")
        }
         case ( day == 5 ) :
        {
          document.write("Today is friday!")
        }
         case ( day == 4 ) :
        {
          document.write("Today is thursday!")
        }
         case ( day == 3 ) :
        {
          document.write("Today is wednesday!")
        }
         case ( day == 2 ) :
        {
          document.write("Today is tuesday!")
        }
         case ( day == 1 ) :
        {
          document.write("Today is monday!")
        }
         case ( day == 0 ) :
        {
          document.write("Today is sunday!")
        }
    }    
}

    </script>
lucromick is offline   Reply With Quote
Old 10-01-2011, 07:49 PM   PM User | #2
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,044
Thanks: 197
Thanked 2,412 Times in 2,390 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
You need to revise your understanding of the syntax of a switch statement.
Also <script type="text/javascript">


Code:
<html>
<head>

<script type="text/javascript">
function showMessage() {
var d = new Date();
var day = d.getDay();     // Note: Sunday=0, Monday=1, etc...
var message = "";
switch(true){
case ( day == 6 ) : message = "Saturday" ; break;
case ( day == 5 ) : message = "Friday" ; break;
case ( day == 4 ) : message = "Thursday" ; break;
case ( day == 3 ) : message = "Wednesday" ; break;
case ( day == 2 ) : message = "Tuesday" ; break;
case ( day == 1 ) : message = "Monday" ; break;
case ( day == 0 ) : message = "Sunday" ; break;
}   

document.getElementById("display").innerHTML = "Today is " + message;
}
</script> 
</head>

<body onload = "showMessage()">
      
<span id = "display" style=color:"red"></span>

</body>
</html>
Prefer to use DOM methods of displaying the information which can be styled using css rather than the crude document.write method.


Correct me if I'm not mistaken - Politician interviewed on BBC Radio Four
__________________

All the code given in this post has been tested and is intended to address the question asked.
Unless stated otherwise it is not just a demonstration.

Last edited by Philip M; 10-01-2011 at 07:53 PM..
Philip M is offline   Reply With Quote
Old 10-01-2011, 08:02 PM   PM User | #3
xelawho
Senior Coder

 
xelawho's Avatar
 
Join Date: Nov 2010
Posts: 2,437
Thanks: 52
Thanked 454 Times in 452 Posts
xelawho will become famous soon enoughxelawho will become famous soon enough
or simpler:

Code:
<script type="text/javascript">
function showMessage() {
var d = new Date();
var day = d.getDay();     // Note: Sunday=0, Monday=1, etc...
var message = "";
switch(day){
case 6: message = "Saturday" ; break;
case 5: message = "Friday" ; break;
case 4: message = "Thursday" ; break;
case 3: message = "Wednesday" ; break;
case 2: message = "Tuesday" ; break;
case 1: message = "Monday" ; break;
case 0: message = "Sunday" ; break;
default : message = "wha?" ; break;
}   

document.getElementById("display").innerHTML = "Today is " + message;
}
</script> 
</head>

<body onload = "showMessage()">
      
<span id = "display" style=color:"red"></span>

</body>
</html>
xelawho is offline   Reply With Quote
Old 10-01-2011, 08:20 PM   PM User | #4
nomanic
Regular Coder

 
nomanic's Avatar
 
Join Date: Feb 2009
Location: United Kingdom
Posts: 252
Thanks: 9
Thanked 33 Times in 33 Posts
nomanic is an unknown quantity at this point
battle for simplest!

Code:
<script type="text/javascript">
function getWeekday() {
     switch((new Date()).getDay()){
     case 6: return "Saturday" ;
     case 5: return "Friday" ;
     case 4: return "Thursday" ;
     case 3: return "Wednesday" ;
     case 2: return "Tuesday" ;
     case 1: return "Monday" ;
     }   
     return "Sunday" ;
};
function showMessage() {
     document.getElementById("display").innerHTML = "Today is " + getWeekday();
};
</script> 
</head>
<body onload = "showMessage()">      
<span id = "display" style=color:"red"></span>
</body>
</html>

Last edited by nomanic; 10-01-2011 at 08:23 PM.. Reason: better spacing
nomanic is offline   Reply With Quote
Old 10-01-2011, 08:31 PM   PM User | #5
ironboy
Regular Coder

 
Join Date: Sep 2011
Location: Sweden
Posts: 154
Thanks: 1
Thanked 22 Times in 22 Posts
ironboy is an unknown quantity at this point
Joining the battle
Code:
function getWeekday() {
  return "Sunday,Monday,Tuesday,Wednesday,Thursday,Friday,Saturday".split(',')[new Date().getDay()]
};

Last edited by ironboy; 10-01-2011 at 08:43 PM..
ironboy is offline   Reply With Quote
Old 10-01-2011, 08:35 PM   PM User | #6
nomanic
Regular Coder

 
nomanic's Avatar
 
Join Date: Feb 2009
Location: United Kingdom
Posts: 252
Thanks: 9
Thanked 33 Times in 33 Posts
nomanic is an unknown quantity at this point
love it!
nomanic is offline   Reply With Quote
Old 10-01-2011, 08:44 PM   PM User | #7
lucromick
New to the CF scene

 
Join Date: Aug 2011
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
lucromick is an unknown quantity at this point
Thank you for all the help guys! I'll use the DOM method from now on.
much appreciated
lucromick is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 10:57 AM.


Advertisement
Log in to turn off these ads.