Due Tuesday, March 4
50 Points
This assignment is meant to help you explore and practice using some of the basic JavaScript syntax we have been covering in lectures. It consists of several relatively simple programs for you to write and test.
Since you will be writing several separate programs, each requiring its own XHTML page (and CSS if you choose to use it), start by writing a simple XHTML page which will eventually provide links to each program’s XHTML page. Make it clear from each link’s label and/or context to which program it leads. Eventually, when the assignment is complete, publish this page (along with all the files comprising your solutions) to the wcit server and add a link to it to your home page.
An important part of learning how to program is learning early on in the process how best to utilize available reference materials. While the book provides useful details on the specific aspects of JavaScript that its authors have chosen to discuss, it is not meant to be an exhaustive reference for the language.
Before you tackle the rest of this assignment, get your feet wet with this learning process by exploring the documentation of the built-in Math object at DevGuru’s JavaScript Quick Reference. You’ll quickly note that the Math object contains a wide selection of properties and methods that provide some very useful mathematical operations and values.
You should look over the Math object as a whole so that you have a sense of what’s available. Most programmers wouldn’t bother to memorize the whole thing, but simply file away the knowledge of what sorts of things are present. That way, when they encounter a need for something the Math object can provide, they’ll hopefully remember where to go to look for it.
Pay particular attention to the PI property and the random(), atan(), tan(), pow(), round() and floor() methods, since they will be needed later in this assignment.
Write a program (and the XHTML file that utilizes it) that will accomplish the following.
Prompt the user to enter their first name, middle name, last name and age (in that order). Use four (and only four) separate prompt dialogs, one for each data item.
Then, display an alert that shows three lines of text. The first line should consist of the user’s first, middle, and last names, in that order. The second line should consist of the user’s last, first and middle names, in that order with a comma after the last name. And the last line should display the text “Your magic number is ” followed by the user’s magic number, which you can calculate by adding their age to the number of characters in their entire name (first, middle and last combined without spaces).
Aside from learning how to make effective use of available references, a good programmer needs to learn how to track down the domain knowledge they need in order to solve a problem. Domain knowledge is different from programming knowledge because it pertains to the problem regardless of how you choose to solve it. For example, domain knowledge is what you would need to know to solve the problem in your head or on paper. Unless you’re already a wiz-bang mathematician, you’re likely to lack the domain knowledge needed to write this program. So before you begin, spend some time in the library, on the Web, or with a mathematician friend collecting the knowledge you need. And don’t forget the knowledge you picked up concerning the Math object, since you’ll need that too!
Write a program that prompts the user for a number, called num below, and then displays an alert listing the following:
In the body of the page, provide citations to your sources of domain knowledge. If they are printed sources, provide standard bibliographic information. If they are Web sources, provide title, author, URL and a functioning link. If they are personal sources, provide a name and some form of contact information.
Users don’t always do what you expect. For example, in the first program you wrote above, a user might click the Cancel button in one of the prompts. Or they might enter a number when you’ve asked for a name, or a word when you’ve asked for their age. Try some of these things with your first program. Some create problems, and others don’t.
When designing and writing a program, a good programmer tries to predict (to the best of her ability) what users might do and address those eventualities. The goal is to write a program that handles the unexpected without crashing. Sometimes this entails telling the user what they’ve done wrong, and giving them a chance to fix it. Sometimes it involves making some sort of assumption that will allow the program to continue. Often it involves experimenting to figure out how to detect certain unexpected results and how to respond appropriately.
Write a second (and separate) version of the first program, modifying it so that it handles the unexpected as follows:
When you’ve got values you can use, proceed to show the same results you did in the earlier program. Be sure to test your program extensively to make sure you’ve properly addressed each issue outlined above.
As you write programs throughout the semester, try to train yourself to look for opportunities to expect the unexpected. Your code will be more complex, but your programs will be much better as a result.
Recall that in the first assignment you were asked to write an algorithm for the following problem:
Explain how to derive an integer in the range low to high, inclusive, from a randomly generated value R. The value R will be a floating point value greater than or equal to 0 and less than 1. The values low and high will both be integers and low < high.
Now you know enough to implement that algorithm in JavaScript. If you had difficulty with the algorithm, feel free to use my version as the basis for your implementation.
Write a program that asks the user to provide values for low and high, such that low < high. Use separate prompts and ensure that you keep asking until you get values you can use. Since the algorithm requires that low and high be integer values, round them to the nearest integer. Once you have acceptable values for low and high, generate five random integers in the range low to high, inclusive, and display them using an alert (one per line).
Programs like this require quite a bit of testing, since the randomness of their operation means you might have to try several times before encountering the situation you are trying to test. Think about ways to maximize the probability of getting the situation you are trying to test and test intelligently.