Alright, we've gotten you through the basics of writing JavaScript. You will now be
writing functions. However, we will not be going through them step-by-step. You will
learn them by answering the problems below. Refer to the slides. If you need
additional help, ask!
For each of the problems below, save them in a separate JavaScript file. Call them lab6_1.js, lab6_2.js, lab6_3.js,
and lab6_4.js. You will be submitting four files. Leave whatever code you used to test
your function in the file. For example, the first problem asks you to write a function called simpleAlert
.
To test it, you would need to call the function. So below the function declaration, you would have the line:
simpleAlert();
When testing each JavaScript file, don't forget to change the filename in the HTML file (see
bold line when you first created the HTML file)!
1. Create a function called simpleAlert
that takes no parameters and displays
an alert box that says Hello!.
2. Create a function called simpleAlert2
that takes one parameter named
msg
and have an alert box display that parameter. For example, if I call your
method like so:
simpleAlert2("Good day!");
a popup box should appear with the message Good day!
3. Create a function called max
that takes two parameters (both numbers)
and returns the maximum of the two numbers. There should be NO call to
alert
within the max
function. How would you test this function?
See the slides on how we tested the computeBMI
function.
4. Create a function called maxOf4
that takes four parameters (all numbers) and
returns the maximum of the four numbers. You may NOT use any conditionals or
relational operators within the maxOf4
function. You are to copy your
max
function declaration from #3 and make calls to max
from inside
maxOf4
.
5. (NO LONGER REQUIRED) Regarding slide #13 on the lecture slides...
Turn-in Instructions
When submitting, make sure you have the right section. Some TAs have two sections, so make sure you know which
of the two sections is the right one. Turn in all 4 files here.