CSci 1001 Lab 3: JavaScript Conditionals

For this lab please team up in pairs

Study the following examples: Introduction to conditionals Combining conditions

Task 1: Using a conditional to show pictures

Copy/paste the source code of this page into a file pictures.html. Add a conditional that, depending on the person's answer, displays one of the two pictures. More specifically:


Task 2: Improving the picture program

Change the program so that the lower-case y and n are also accepted as an answer. Do not add extra if/else, use || (means "or") instead. You need to write out both conditions and then connect them with ||. For instance,

if (n == 4 || n == 5) // correct
is correct, but this is wrong:

if (n == 4 || 5)  // WRONG!

Task 3: Fortune-teller

Write a program that uses a random number generator (see here) to generate a random number between 1 and 5. Then display one of six fortune messages, depending on the number. For instance, if the number is 2, your message might be "This is the luckiest day of your life!". Run your program several times to make sure it works.

Task 4: Unfair fortune-teller

Change your fortune-teller to increase the range of your numbers and make some messages more frequent than others by making them show up for more than one number. For instance, 1, 2, and 3 may all give you the same message, 4 and 5 give you a different one, and so on. Make sure that even the least frequent messages show up if you try your program long enough.

At the end of the lab

CSci 1001 course web site.

The views and opinions expressed in this page are strictly those of the page author. The contents of this page have not been reviewed or approved by the University of Minnesota.