CMSC104 Spring 2011 Functions Exercise

Objectives

-To practice writing different types of functions

Note: There is nothing that you must turn in associated with this exercise. If you do not finish the exercise during the lab session, it is to your benefit to finish it later on your own.

The Assignment

The purpose of this exercise it to get some hands-on practice with writing simple Javascript functions.

Do each of the following exercises in order. Each one builds upon the solution to the one before it. The extra-credit option for the last exercise is challenging, so if you solve it, you will receive actual points towards your grade. (It will count as a 10-pt bonus towards Project 3). I will give the solution at the end of class, and record the exact time, so do not edit it after I say not to, or you will not receive the extra points.

The Programming Tasks

    For each task, write a function to do it, then call it from inside your section:
  1. A function that outputs "Hello, world" with an alert box
  2. A function that takes 1 parameter, and outputs it with an alert box.
  3. A function that takes 2 numerical parameters, and outputs the sum with an alert box
  4. A function that takes 2 numerical parameters, and returns their sum; caller should output result with an alert box.
  5. A function that computes the "factorial(x)" function, and returns the value of "X!"; caller should output answer.
  6. Extra Credit: write "factorial(x)" without using loops!

Creating the Directory for the Exercises

  1. Log into your gl account.

  2. Change into your '../pub/www' directory (note: NOT the 'cs104' subdirectory!):

    linux1[2]% pwd
    /afs/umbc.edu/users/d/b/dblock/home   
    linux1[3]% cd ../pub/www
    
    

  3. Make a new directory called 'cs104-lab5', and 'cd' into it:

    linux1[4]% mkdir cs104-lab5
    linux1[5]% cd cs104-lab5
    
    

  4. Now, use xemacs to create files for your lab exercises, using names of the form "lab5-1.html", "lab5-2.html", etc. Don't create them all at once! First use xemacs to create the solution to the first exercise:


    linux1[6]% xemacs lab5-1.html
    
    

  5. After you've written and debugged lab5-1.html, you can use it as a template to solve the next one, by copying the file, and editing the copy:

    linux1[7]% cp lab5-1.html lab5-2.html
    linux1[8]% xemacs lab5-2.html
    
    

  6. Repeat this copy-and-edit process for the other exercises (obviously changing the file names appropriately!).

If you do not complete the lab today and would like to finish later, you can continue to work on it from home.

If you did the extra credit (exercise #6), leave the file "lab5-6.html" untouched after I call "time" in lab; if you edit it after that, you will not receive the points.

Last Modified: Tuesday, 03-May-2011 15:33:36 EDT