CMSC 104 Spring 10
Arrays Lab
Objectives
- To practice working with arrays, functions, and random numbers in JavaScript
- To lean to work with images on a web page
The Lab Assignment
In this lab we will work with arrays and images, for-loops, random numbers, and functions. You should be able to complete the lab during the class time, but if
not, that's ok. Try to complete the lab on your own as you prepare for the final exam.
The lab assignment is broken into the following steps.
- Create a directory under your
pub/www/cs104
directory for this lab named
arrays
and navigate to that directory so that it is your working directory.
- You will find the image files for six dice which represent the values 1- 6 in Mr. Frey's public directory --
/afs/umbc.edu/users/f/r/frey/pub/die-images
. Copy these files to the directory you created in the step above using the command
cp /afs/umbc.edu/users/f/r/frey/pub/dice-images/* .
- Create a web page named
dice.html
with the following functionality
- Prompt the user for the number of dice he/she wishes to roll. This should be a number between 1 and 5, inclusive.
- For each roll, generate a random number between 1 and 6, inclusive and display the corresponding die image
- Display the sum of the dice
Helpful Hints
- To generate a random integer between 1 and 6 (inclusive) use the formula
randomInt = Math.ceil( Math.random( ) * 6);
- Store the image file names in an array. The index into the array is based on the random number generated above.
Consider writing a function that returns the image file name.
- To insert an image into a web page, use the
img
tag as shown in the example below.
- You may assume the user will input a positive integer when asked for the number of rolls.
It is not necessary to perform input validation.
- Your code should display the die images on a single line with the total underneath the images.
- Screen shots are provided below.
Sample Screen Shots
Last Modified: Tuesday, 11-May-2010 14:19:24 EDT