View Full Version : Using a time value to generate a text response

11-22-2010, 01:25 AM
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" />



Old Pedant
11-22-2010, 01:57 AM
You can't do two comparisons in a single operation.

Must do it as

if ( a > b && a < c ) ...

Philip M
11-22-2010, 09:39 AM
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.