Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New to the CF scene
    Join Date
    Mar 2013
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Customized greeting based on time of the day

    Hi guys,

    I am new to Javascript and want a greeting on my Page that either says Good morning/Good day/Good afternoon/Good evening to the user, depending on what time of the day it is. Also, the user is entering his/her name in a prompt box so that the name can be included in the greeting. For example "Good morning -name here- ! "

    I started coding this, and got so far :

    <html>
    <head>

    <title>My first script</title>
    <script type="text/javascript">

    var user_name=prompt("Please enter your name","Enter your name here")


    </script>

    </head>

    <body>
    <script type="text/javascript">
    <!--

    var myDate=new Date()
    var hrs= myDate.getHours()


    if(hrs =>5 && hrs <=11) {
    document.write("Good morning " + user_name + "!");

    }
    else if (hrs > 11 && hrs <=14) {

    document.write("Good day " + user_name + "!");

    }

    else if (hrs >14 && hrs <=18) {

    document.write("Good afternoon " + user_name + "!");

    }

    else if (hrs > 18 && hrs<23 ) {

    document.write("Good evening " + user_name + "!");
    }

    else {
    document.write("Go to bed " + user_name + "!");
    }




    //-->
    </script>


    </body>

    </html>

    This does not work though.. What am I doing wrong? I was not sure if the prompt should go in the head or body section.

    Have a nice day!

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,898
    Thanks
    203
    Thanked 2,530 Times in 2,508 Posts
    Nowadays scripts are normally placed right in front of the </body> tag.

    Your fatal error is here: - if(hrs =>5 // should be >=

    Here is your script reworked:-

    Code:
    <html>
    <head>
    <title>My first script</title>
    </head>
    
    <body>
    
    <script type="text/javascript">
    
    var user_name = prompt("Please enter your name","");
    
    var myDate =new Date();
    var hrs = myDate.getHours();
    
    if ((hrs >= 5) && (hrs <=11)) {
    document.write("Good morning " + user_name + "!");
    }
    else if ((hrs > 11) && (hrs <= 14)) {
    document.write("Good day " + user_name + "!");
    }
    else if ((hrs > 14) && (hrs <= 18)) {
    document.write("Good afternoon " + user_name + "!");
    }
    else if ((hrs > 18) && (hrs < 23 )) {
    document.write("Good evening " + user_name + "!");
    }
    else {
    document.write("Go to bed " + user_name + "!");
    }
    
    </script>
    
    
    </body>
    
    </html>
    Be aware that prompts are obsolete, as is document.write(). I do appreciate that this is your first script, but you should really start on the right track and use DOM methods to obtain input from the user and display messages. Then you could style the message (color, font-face,font-size, font-weight etc.) to improve the appearance of the display.

    The <!-- and //--> comment (hiding) tags are also long obsolete and have not been necessary since IE3 (i.e. since September 1997).

    You may care to consider what should happen if the user enters his name as "" (blank) or a series of spaces, or something silly like 578&432.
    One way to prompt repeatedly until a valid name is entered:-

    Code:
    for (var i=1; i<2; i++) {
    var ans = prompt ("Please enter your name here", "");
    ans = ans.replace(/^\s+|\s+$/g,"");  // strip leading and trailing spaces
    if ((ans == null ) || (ans == "") || (/[^a-z\s\-\']/gi.test(ans))) {
    alert ("You must enter your name using only letters, space, hyphen and/or apostrophe!! ");
    i -- ;
    }
    }


    The Moving Finger writes; and, having writ,
    Moves on: nor all your piety nor wit
    Shall lure it back to cancel half a line,
    Nor all your tears wash out a word of it.

    -- Omar Khayyam
    Last edited by Philip M; 03-30-2013 at 08:17 PM.

    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.

  • #3
    Senior Coder xelawho's Avatar
    Join Date
    Nov 2010
    Posts
    2,883
    Thanks
    56
    Thanked 539 Times in 536 Posts
    avoiding all the if/else runaround...

    Code:
    <script type="text/javascript">
    var user_name=prompt("Please enter your name","Enter your name here")
    var times = [" to bed", "od morning", "od afternoon", "od evening"];
    var h = new Date().getHours();
    document.write("Go" + times[(Math.ceil((h + 25) / 6) - 5)]+", "+user_name)
    </script>

  • #4
    Regular Coder
    Join Date
    Jan 2013
    Location
    Germany
    Posts
    578
    Thanks
    4
    Thanked 77 Times in 77 Posts
    I had to laugh at how you extracted the shared 'Go'.

  • #5
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,898
    Thanks
    203
    Thanked 2,530 Times in 2,508 Posts
    Quote Originally Posted by xelawho View Post
    avoiding all the if/else runaround...

    Code:
    <script type="text/javascript">
    var user_name=prompt("Please enter your name","Enter your name here")
    var times = [" to bed", "od morning", "od afternoon", "od evening"];
    var h = new Date().getHours();
    document.write("Go" + times[(Math.ceil((h + 25) / 6) - 5)]+", "+user_name)
    </script>
    Hmm. It is the guy's first script!

    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.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •