HW 8

in-class section: due 5/24 (by midnight)
DL section: due 5/25 (by midnight)


We talked more about conditionals (chapter 11). Next week, we'll discuss algorithms and repetition.


1. [Random Coin Toss, 20pt] Implement a web-page that randomly tosses a coin and keeps track of the total number of heads and the total number of tails seen (in two separate input fields). More precisely: the web-page should contain a button that, when pressed calls a function that

When writing the page, remember the random die example, and build the page one feature at a time: first add random selection (test with window.alert), then bring up the corresponding picture, then increase the counter. Make sure the previous stage works before  you go on to the next.

2. [conditional, 20pt] Beethoven wrote 32 piano sonatas, four of which he gave names to: No. 8 (Pathetique), No.21 (Waldstein), No. 26 (Das Lebewohl), and No. 29 (Hammerklavier). You want to write a web-page on which the user can enter the number of a sonata into a text-input field, and see the name of the sonata (if it has one) in another text-input field (disabled, for output only). If the sonata doesn't have a name, the field should remain empty.

Hint:  This does not require 32 ifs, just four. You do not have to check that the number entered is actually between 1 and 32.

Extra Credit: verify that the number entered is between 1 and 32. If not, display "not a valid input" in the field you are using for output.

3. [Extra Credit] Finish the leap year problem we started in class. Your program should identify leap years according to the full rule: a year is a leap year if it is divisible by 4, unless it is divisible by 100 in which case it also has to be divisible by 400.

4. [Extra Credit] Do the leap year example using a single if.

5. [Extra Credit] Extend your program for #1 (do this on a separate page) to tell you whether you've seen more heads or tails so far (could be as a piece of text or a separate picture that shows which side of the coin is leading the count).


Marcus Schaefer
Last updated: May 17th, 2006.