CSci 1001 Lab 4

Due Monday Nov 4 at 11:59pm.

Introduction to JavaScript

Background

Carefully study the examples on the resources page: Javascript conditionals and combining conditionals.

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:

Hints:

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 (extra credit): 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


This page is a part of CSci 1001 course web site.