Given an HTTP status code, print its corresponding status description.




The World Wide Web (WWW), as you may already know, is backed by the HTTP protocol. It relies on a very intuitive and simplistic model of a client and server.

A client, such as a web browser, sends a request for a resource to a server that holds the resource. This request is formally called an HTTP request. Similarly, once the server receives the request and understands what exactly is asked in it, it then responds with an HTTP response.

Each HTTP response has a corresponding status which is comprised of a code and a description.

The status of an HTTP response is meant to specify what happened with the request, for e.g. whether it was responded to successfully, or whether there was an error in the request, or maybe an error in the server's code, and so on and so forth.

A status code, by that means, is the formal way to tell what happened with the request. It's a 3-digit number where the first digit represents the general class that the code belongs to.

For instance, all 2XX codes such as 200, 201, 202 imply 'success'. Similarly, all 4XX codes, such as 401, 402, the well-known 404, represent 'client error'. The list of possible codes is defined by RFC 9110.

Each status code has a corresponding description to make it easy for people to understand the meaning of the status code in HTTP responses, rightaway. For example, 404 has the description Not Found, which implies that the requested resource doesn't exist on the server.

All the five HTTP status code classes also have corresponding descriptions so that we can get a general idea, just by looking at a status code, of what happened with the sent request.

As defined by RFC 9110, here's the list of all the five classes:

  • 1XX: Informational
  • 2XX: Successful
  • 3XX: Redirection
  • 4XX: Client Error
  • 5XX: Server Error

In this exercise, you have to create a program that asks the user to enter a status code and then outputs a string representing the class that the code belongs to.

The status code must be obtained via an input prompt and then the corresponding output be made via an alert message.

Keep in mind that it's possible for the user to enter some arbitrary text into the prompt, likewise you MUST validate the input as follows:

  1. If the length of the text is not 3, alert 'Invalid code length.'
  2. Otherwise, if either of the three characters is not a digit, alert 'Each character must be a digit.'
  3. Otherwise, if the first digit is not in the range 1-5, alert 'First digit not in range 1-5.'


As an example, suppose we enter the text '301' into the input prompt. The output should then be 'Redirection'. Similarly, if we enter '600', the output should be 'First digit not in range 1-5.'

Note that your solution MUST use the switch statement.

And you are obviously free to create as many helper functions as you want to, as long as they improve the overall readability and simplicity of the code.

Remember that we haven't structured this exercise to literally pinpoint out-of-range status codes for each code class. For instance, the code 250 isn't defined in RFC 9110, yet it'll work absolutely fine in this program. So, if you want to, you could maybe take the exercise a step further and even check whether the input status code is in-range for its respective class.