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

    Using a time value to generate a text response

    Hi, I am stumped with this question and hope someone can help me. I am very new to javascript and am having a lot of trouble. Here is the question I am trying to solve along with the script that I have so far. Can someone please help me out, thanks!!

    In a Prompt Box, ask the user “When do you plan to come to the
    Hatch to eat?” and expect the user to provide the time in military time (e.g., 0400 =
    4 am and 1700 = 5 pm). If the hour is between 7 am and 10:59 am (0700-1059),
    reply using an Alert Box “The Hatch is Serving Breakfast!” If the hour is between
    11 am and 3:39 pm (1100-15:59), reply “The Hatch is Serving Lunch!” If the hour is
    between 4 pm and 10 pm (1700-2200), reply “The Hatch is Serving Dinner!” If the
    hour is between 10:01 pm and 11 pm, reply “The Hatch is serving coffee and tea!”
    Otherwise reply “The Hatch is closed after 11 pm!”

    My Script:

    <script language=javascript>


    var time=prompt("What time is it? (Please enter in military time with no symbols) ","");

    if( time < 1600 ){
    document.write("The Hatch is Serving Breakfast! ");

    if( 1600 < time < 1059 > ){
    document.write("The Hatch is serving Lunch! ");

    if( time < 1659 > 2159 ){
    document.write("The Hatch is serving Dinner! ");




    <input type="button" onclick="show_prompt()" value="Show prompt box" />



  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Thanked 4,741 Times in 4,703 Posts
    You can't do two comparisons in a single operation.

    Must do it as

    if ( a > b && a < c ) ...
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #3
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    London, England
    Thanked 2,573 Times in 2,551 Posts
    Clearly this is homework. But you need to consider what will happen if the user types "Zamboni" in the prompt box. Or types nothing at all (test your code and see what happens!)

    The value entered by the user into the prompt box is a string, not a number. To perform comparisons correctly you need to convert it to a number.

    var time=prompt("What time is it? (Please enter in military time with no symbols) ","");
    var message = "";
    time = parseInt(time);
    if (time < 700) { message = "The Hatch is closed" }  // note 700 not 0700
    if (time >= 700 {message = "The Hatch is serving breakfast"}
    if (time >= 1100) {message = "The Hatch is serving lunch"}
    // and so on 
    The user is supposed to give a response to the prompt
    "When do you plan to come to the Hatch to eat?"
    so the prompt "What time is it?" is hardly sensible.

    onclick="show_prompt()" // there is no such function

    I do realise that you are a beginner, but you need to be aware that document.write statements must be run before the page finishes loading. Any document.write statement that runs after the page finishes loading will create a new page and overwrite all of the content of the current page. So document.write is at best really only useful to write the original content of your page. It cannot be used to update the content of your page after that page has loaded.

    <script language=javascript> is long deprecated and obsolete. Use <script type = "text/javascript"> instead.


    Posting Permissions

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