Previous page
Next page

8.2 Overview of Watson Javascript

The imperative paradigm is based on the idea that a program is sequence of commands or instructions that the computer is to follow to complete a task. The imperative style of programming is the oldest, and now with object-oriented extensions, continues to be far and away the most popular style of programming.

Watson JavaScript is an imperative programming language based on JavaScript. JavaScript is a popular programming language that is often used for writing small programs that are embedded in web pages. Most web browsers, including Microsoft’s Internet Explorer and Mozilla’s FireFox, can run these programs.

Watson JavaScript is an “extended subset” of regular JavaScript. By “extended” I mean that Watson JavaScript requires programmers to specify some additional information that full JavaScript does not, such as the type of a variable upon its declaration. By “subset” I mean that Watson JavaScript does not include all of the features of full JavaScript – though it does include all of the basics, and is a complete, general-purpose programming language.

One way of thinking about writing Watson JavaScript programs is to compare it to programming full JavaScript with “training wheels” on. Every program you write in Watson JavaScript can be run on most web browsers without modification, but there are many things programmers are allowed to do in full JavaScript that are forbidden in Watson JavaScript. For example, Watson JavaScript forces programmers to do certain things in certain ways, such as declaring all variables at the top of a program, where full JavaScript allows much more flexibility. These restrictions are designed to help beginning programmers avoid making common mistakes.

Figure 8.1 illustrates the general format of a Watson JavaScript program and shows how such a program could be embedded in a simple web page. The first thing to notice about this figure is that much of the text is shown in gray. This gray text is not JavaScript code but is instead HTML, or HyperText Markup Language. In order to run Watson JavaScript programs directly in a web browser, they would need to be embedded within the HTML tags shown in gray. Conversely, the HTML can be omitted when Watson JavaScript programs are created and run within the Watson JavaScript Lab environment available at http://watson.latech.edu.

Figure 8.1: The format of a Watson JavaScript program embedded within a web page

HTML is the language used to write web pages. While the purpose of this chapter is not to teach HTML, I will say a few words about this subject for those of you who might be interested in what all that gray text means.

HTML pages are text documents “marked up” with a variety of “tags” that define the structure of the document. Most tags come in pairs such as <html> and </html>. Every valid web page begins with the <html> opening tag and ends with the </html> closing tag. All of the text between these two tags, including other pairs of tags, is part of the HTML document.

HTML documents generally consist of two parts, the “head” and the “body”. The head is optional and is omitted in Figure 8.1. The body of an HTML document resides between the opening <body> and closing </body> tags.

One of the primary jobs of web browsers is to format the contents of HTML documents and present those documents in an attractive manner. For our purposes, however, we want to turn this feature off so that the output of our Watson JavaScript programs will be presented to the user in exactly the form we specify. Thus, our JavaScript programs will be embedded within <pre> and </pre> tags. These tags tell the web browser that the output has been pre-formatted and should be displayed without modification.

The <script type=”text/javascript”> and </script> tags surround the actual JavaScript program. Everything within these tags will be JavaScript code.

Watson JavaScript programs consist of two parts, declarations and imperative statements. The declarations define the objects that will be used by the program, while the imperative statements specify how those objects will be manipulated in order to accomplish the actions of the program.

var name ; // type

Figure 8.2: The two kinds of objects (variables and functions) that can be declared in Watson JavaScript Programs

Figure 8.2 illustrates the two kinds of objects that can be declared in Watson JavaScript programs. These objects are variables and functions. You may recall from your study of the Watson Graphics Language that variables are named objects that can hold a value of a particular type. Functions are subprograms – programs that occur within other programs. If this idea seems strange at the moment, don’t worry. The usefulness of functions will become apparent as we proceed through this chapter.

Figure 8.3 presents all of the statements types available in Watson JavaScript. Every Watson JavaScript program that can ever be written will be formed from these basic building blocks, along with the declarations of Figure 8.2. Surprisingly, this small number of statements is all that is required to give Watson JavaScript the power of a general-purpose programming language.

Watson JavaScript statements can be grouped into seven general categories. These categories are: output statements, input statements, variable assignment, selection statements, iteration statements, function invocation, and function return. Each of these categories and the details of the statements that occur within them are discussed below.

As mentioned above Watson JavaScript is an extended subset of true JavaScript. Watson JavaScript is not intended as a replacement for full JavaScript – or Java or C++ for that matter. Instead, Watson JavaScript is a teaching tool aimed at simplifying the process of learning to program. Watson JavaScript purposefully omits many features available in full JavaScript and other popular languages, in order to keep the language from becoming overly complex. In addition, the Watson lab that you will most likely use to create your Watson JavaScript programs incorporates a point-and-click interface similar to the one provided by the Watson Graphics Lab. The Watson JavaScript language and its implementation in the lab are designed to help beginning students focus on “the big picture” rather than get bogged down in the complexities inherent in “real” languages and their program development environments.

Output Statements:

Input Statements:

    Text input:
    variable = prompt(message,defaultValue);
    Numeric input:
    variable = parseFloat(prompt(message,defaultValue));

Assignment Statement:

Selection Statements:

  • if (expression relationalOperator expression)
  • {
    • statements to be executed if expression is true
  • }
  • else
  • {
    • statements to be executed if expression is false
  • }
  • if (expression relationalOperator expression)
  • {
    • statements to be executed if expression is true
  • }

Iteration Statements:

Function Invocation:

    functionName(inputs);

Function Return:

    return(expression);

Figure 8.3: The Watson JavaScript program statements.


Return to top