In this lab, you will explore some of the basic features of the JavaScript environment. In particular, you will write code and execute it in the JavaScript interpreter, and locate information in the JavaScript reference pages provided. You will also work with the Text Editor to save copies of your code and print a listing for submission with the Lab.
One of the overriding themes of this course is that programming is not an end to itself, but a tool for experimentation and problem-solving. As an example of this, you will consider several experiments with random word generation, using JavaScript as a tool in your analysis.
For example, if you entered the following JavaScript code in the text box
When entering your code, you are free to use the arrow keys to move around and the backspace key to erase erroneous code. To erase an entire section of code, you can highlight the code (hold down the left mouse button and drag the cursor over the section of code) and then hit backspace. Note that if there are errors in your code, you can always go back and fix them and try again. The contents of the text box persist after executions, so you can repeatedly edit and execute the same piece of code.
For each of the following alterations to the write statement above, note whether the result is an error. That is, make the specified modification and attempt to execute the statement in the JavaScript interpreter. If an error occurs, try to explain why you think it occurred. If there is no error, note whether the displayed message is changed in any way.
Based on these examples, generalize as to when capitalization and spaces are allowed, and what their effect is in JavaScript statements such as this one.
While you do not know much about JavaScript yet, you can still get some practice accessing these pages. Search the JavaScript Language reference page to answer the following questions:
At the bottom of the class links are two links labeled JavaScript Guide and JavaScript Reference. These links will take you to the official documentation for JavaScript maintained by Netscape, providing detailed descriptions of the JavaScript language and its features. You may explore these documents at your leisure throughout the course. Clearly, there is much more to the JavaScript language than we are going to cover in one semester. However, the subset of JavaScript that you will be learning is sufficient for solving many challenging and interesting problems. You are certainly encouraged to push the limits of your knowledge via these documents.
Once you have your code debugged (the computer term for error-free), execute it five times and list the letter sequences that were generated.
In order to save your code so that you can reload and execute it in the future, you will need to use a text editor. If you are working in your dorm room, or at any other outside location, you can use whatever text editor is handy. On the computers in South 4, there is a simple text editor available under the Personal Applications menu. By clicking on its icon, you will open an editor window containing a blank document. As you write and test code in the JavaScript interpreter, you can save it by copying-and-pasting it into this document. (To copy-and-paste, highlight the contents of the text box and hit Alt-C, then place the cursor in the text editor document and hit Ctrl-V.) You can also copy-and-paste from the text editor into the JavaScript interpreter by highlighting the text in the editor and pressing Ctrl-C and then placing the cursor in the interpreter and pressing Alt-V. (Notice that the Alt key is used in the interpreter and the Ctrl key is used in the editor.)
To save a document in the Text Editor, select save under the File menu. You will be prompted for a file name, and the contents of the document will then be saved under that name. Once you have saved the document, a document icon with that name should appear in the File Manager window. To reopen that document at a future time, you can simply double click on the icon, or else select Open from the File menu in the Text Editor.
You are now prepared to use the JavaScript code to perform some experiments with random letter sequences. In particular, you will use this code as a tool for verifying or disproving hypotheses about word distributions, and to generate further data for analysis.
First, consider the total number of unique 3-letter sequences that can be generated. Since each of the three positions in the sequence can be any of the 26 letters, there are 263 = 17,576 different sequences. Clearly, not all of these sequences form real words. The question arises: how many random 3-letter sequences would you expect to have to generate before you obtain a word? Or, in JavaScript terms, how many times would you have to execute your code before a word is displayed?
It so happens that there are roughly 550 3-letter words in the English language (at least according to the UNIX online dictionary). Thus, if you generated a random 3-letter sequence, there is a 550 out of 17,576 chance that it will be a word. Since 550/17,567 is approximately 1/32, you should get a word 1 out of every 32 or so times. In other words, you would have to generate approximately 32 random 3-letter sequences to obtain a word. This number can be verified experimentally using your JavaScript code.
As the length of the letter sequences increases, the chances of generating a word at random decreases dramatically. For example, there are 1,777 4-letter words in the online dictionary. Thus, the chances of generating a 4-letter word at random are 1 in 257 (264/1,777 = 456,976/1,777 = 257). For 5-letter sequences, the chances of generating a word at random is 1 in 4,920 (265/2,415 = 11,881,376/2,415 = 4,920) .
Part of the blame for the scarcity of words among randomly generated sequences falls on letters such as 'q' and 'z'. Since these letters are used so infrequently in English, their inclusion in a random sequence of letters makes a real word extremely unlikely. If we exclude letters such as these, however, we can improve the chances of generating words considerably. For example, the 10 letters that appear most frequently in English text are "etaoinshrd". Random sequences of these letters would appear more likely to produce words.
Show your work in obtaining your estimate. Note: you can use the JavaScript interpreter to help in the computation, e.g.,
Cut-and-paste a copy of your modified code at the bottom of your Lab1 document, below the original version. Hand in a printout of your Lab1 file, attached to these sheets.