# JavaScript Switch Statement?

**URL:** <https://forum.kirupa.com/t/javascript-switch-statement/305576>\
**Category:** web dev\
**Created:** [February 27, 2010, 6:14pm UTC](https://forum.kirupa.com/t/javascript-switch-statement/305576 "2010-02-27T18:14:21Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![boebarl1](https://avatars.discourse-cdn.com/v4/letter/b/3e96dc/32.png) [@boebarl1](https://forum.kirupa.com/u/boebarl1)\
**Post date:** [February 27, 2010, 6:14pm UTC](https://forum.kirupa.com/t/javascript-switch-statement/305576/1 "2010-02-27T18:14:21Z")

</div>

I am a beginner in programming with javascript and I am having a problem with shortening up this switch statement? I want the user to be able to type in a hour of the day from 1-24. when you type 5-11 script runs good morning, 12-18 script runs good afternoon, 19-24 and 1-4 script runs good evening. My code works but it is way to long, if someone can give me a tip, I would really appreciate it. thanks alot.

\<?xml version = “1.0” encoding = “utf-8”?\>  
\<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Strict//EN”  
“[http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd](http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd)”\>

\<!-- XHTML Template --\>  
\<html xmlns = “[http://www.w3.org/1999/xhtml](http://www.w3.org/1999/xhtml)”\>  
\<head\>  
\<title\>Time of Day\</title\>  
\<script type =“text/javascript”\>  
\<!–

```
var time; // user's choice
var validInput = true; // indicates if input is valid
var greeting; // type of list as a string

time = window.prompt( "Please type in what hour of the day it is.

```

(example: 1 - 24)" );

```
switch ( time )
{
case "5":greeting = "&lt;h1&gt;Good Morning!&lt;/h1&gt;";
     break;

case "6":greeting = "&lt;h1&gt;Good Morning!&lt;/h1&gt;";
     break;

case "7":greeting = "&lt;h1&gt;Good Morning!&lt;/h1&gt;";
     break;

case "8":greeting = "&lt;h1&gt;Good Morning!&lt;/h1&gt;";
     break;

case "9":greeting = "&lt;h1&gt;Good Morning!&lt;/h1&gt;";
     break;

case "10":greeting = "&lt;h1&gt;Good Morning!&lt;/h1&gt;";
     break;

case "11":greeting = "&lt;h1&gt;Good Morning!&lt;/h1&gt;";
     break;

case "12":greeting = "&lt;h1&gt;Good Afternoon!&lt;/h1&gt;";
      break;

case "13":greeting = "&lt;h1&gt;Good Afternoon!&lt;/h1&gt;";
      break;

case "14":greeting = "&lt;h1&gt;Good Afternoon!&lt;/h1&gt;";
      break;

case "15":greeting = "&lt;h1&gt;Good Afternoon!&lt;/h1&gt;";
      break;

case "16":greeting = "&lt;h1&gt;Good Afternoon!&lt;/h1&gt;";
      break;

case "17":greeting = "&lt;h1&gt;Good Afternoon!&lt;/h1&gt;";
      break;

case "18":greeting = "&lt;h1&gt;Good Afternoon!&lt;/h1&gt;";
      break;

case "19":greeting = "&lt;h1&gt;Goood Evening!&lt;/h1&gt;";
      break;

case "20":greeting = "&lt;h1&gt;Goood Evening!&lt;/h1&gt;";
      break;

case "21":greeting = "&lt;h1&gt;Goood Evening!&lt;/h1&gt;";
      break;

case "22":greeting = "&lt;h1&gt;Goood Evening!&lt;/h1&gt;";
      break;

case "23":greeting = "&lt;h1&gt;Goood Evening!&lt;/h1&gt;";
      break;

case "24":greeting = "&lt;h1&gt;Goood Evening!&lt;/h1&gt;";
      break;

case "1":greeting = "&lt;h1&gt;Goood Evening!&lt;/h1&gt;";
      break;

case "2":greeting = "&lt;h1&gt;Goood Evening!&lt;/h1&gt;";
      break;

case "3":greeting = "&lt;h1&gt;Goood Evening!&lt;/h1&gt;";
      break;

case "4":greeting = "&lt;h1&gt;Goood Evening!&lt;/h1&gt;";
      break;

default:
    validInput = false;
    
} //end switch

if ( validInput == true )

{document.writeln( greeting );}

```

else  
document.write("\<h1\>Please enter a correct time Knucklehead!\</h1\>");

```
// --&gt;
&lt;/script&gt;

```

\</head\>

\<body\>  
\<p\> Click refresh or reload to run the script again \</p\>  
\</body\>  
\</html\>
