Prev << >> On to the next chapter: Mutating Madlibs.
Can a computer write a poem? We begin with a brief poem (written by me, not a computer) in HTML:
Every web browser renders pages in HTML, a language that organizes text using tags in angle brackets as shown here.
The HTML tags <h2> and <p> define purpose and formatting: <h2> is "heading 2" and <p> is "paragraph". Most HTML elements are made with matching begin and end tags. For example, the <center> tag matches the </center> tag; together they form an element that centers a range of text and nested elements.
Elements can have attributes such as the style attribute here that specifies big 64-pixel margins on the main <body> of the HTML page.
There are about 90 different HTML elements (full list) and more than 100 built-in attributes (full list). Styles can set more than 100 properties other than "margin" (full list). But there are only a few elements, attributes, and styles the we need to understand to get started.
There are two important HTML elements that are not shown in this example: the <script> and <style> elements, used to embed two other languages into HTML.
The next example shows how <script> is used, and <style> is explained in the next chapter.
On these pages I have provided buttons to let you edit the HTML examples or download them directly, but wherever you are on the web, you can always view, download and edit HTML source code. All you need to do is find the "View Source" menu in your browser. Once you see the source, you can make a copy and edit it using a plain text editor like Notepad or WordPad on Windows, TextEdit on Mac, or a free editor like Notepad++.
To have the computer select words for the poem we will need to write some code. Here is an example using the <script> element.
On this page, a couple of the words have been replaced using scripts. These scripts run while the page loads.
The first script defines a function called choose that acts upon a list of words (the parameter we happen to call "list"). The function has two steps:
The second script calls the previously-defined choose function, passing it a specific list of words within square brackets. Calling the choose function in the script in the middle of loading causes a randomly chosen word to be written straight into the document right where the script appears, after the word "A" and before the words "Nautical Poem."
The third script is similar (just different words).
When you open this HTML page, all the scripts run instantly as the page is loaded, and you see a custom poem created just for you. Try running it more than once.
When we define the choose function, the code does not execute right away, but only later when the function is called. The function definition defining choose has four parts:
The function call has two parts:
When calling a function, the parentheses are not optional, so you cannot call "choose[...]" - you must call choose(...). Even if you want to call a function with no arguments, you must use an empty pair of parentheses to indicate you are making a call, rather than just talking about the function itself as an object.
The use of functions to define and defer execution seems simple, but functions are the most important concept in computer science.
Javascript provides many built-in functions that do useful things. For example, to choose a random number we used two functions from the built-in Math object:
Can you see why the following expression assigns to n a random integer from 0 to list.length-1?
var n = Math.floor(Math.random() * list.length);
Another built-in object is the document object. Here we have used the document.write function:
The line of code below writes the the randomly selected word into the document.
The square brackets in list[n] mean "look up the nth item in the list array." Elements in javascript arrays are numbered starting with #0 and their last item is #length-1, so the way we have chosen n to range from 0 to length-1 is perfect.