DISC 3371 -- Final Spring 2003 -- Parks -- Answers |
c1right.gif c1left.gif c2.gif c3.gif c4.gif |
1. (50 points) The 5 gif files shown to the right are used to make a 4 image train appear on the page. When train is moving to the right, it is composed of the 4 images: c4.gif, c3.gif, c2.gif and c1right.gif placed side-by-side. The images appear on the page in this exact left to right order. When the train is moving to the left, it is composed of the 4 images c1left.gif, c2.gif, c3.gif and c4.gif placed side-by-side. The images appear in this exact left to right order. Each image is 90 pixels wide. A global variable named status is used to store the direction of movement of the train. When moving to the right status="R" and when moving to the left status="L". A button labelled "MOVE" always appears on the page. When this button is clicked a sub named move_it is called. This sub:
1. moves each of the four images at the top of the page 10 pixels to the right when status="R" or 10 pixels to the left when status="L". 2. then checks to determine if it is time to reverse the train's direction: If the train is moving to the right and the right-hand edge of c1right.gif" is now >= 720 a sub named set_for_left is called. This sub: (a) sets status="L" and (b) sets the top and left positions of the gifs according to columns c and d in the table below. If the train is moving to the left and the left-hand edge of c1left.gif" is now <= 0 a sub named set_for_right is called. This sub: (a) sets status="R" and (b) sets the top and left positions of the gifs according to columns a and b in the table below. When the page is loaded, a sub named start_it is executed. This sub: (1) sets status="R" and (2) calls the sub set_for_right.
|
1. <DIV id="train_out"></DIV>
2.<DIV id="bp"><input type="button" value="TRY" onClick="try_it()"></DIV>
When the button labelled "TRY" is clicked, the function try_it is executed. This function generates four random numbers between 1 and 5 inclusive (i.e., 1,2,3,4 or 5). The five possible values correspond to one of the five gif files in the last 5 rows of the table shown in problem 1 above. The function creates a string named train. This string contains the HTML to display four gif files side-by-side. The specific gif file names used in this string are determined by using the four random numbers generated. If the random number is 1, then the gif file is from the row numbered 1 above -- c1right.gif, if the random number is 2, then the gif file is from the row numbered 2 above -- c1left.gif, if the random number is 3, then the gif file is from the row numbered 3 above -- c2.gif, etc.
Once the four random numbers have been generated and the gif file names determined, the HTML for the four side-by-side images is created and stored in the string train. The string train is then displayed in the DIV block with id="train_out". If the sequence of 4 random numbers produces one of the two correctly arranged trains (i.e., the random number sequence is 5-4-3-1 or 2-3-4-5) the document's background color is set to red and the DIV block with id="bp" is set to a single blank. Write the javascript for the function try_it.