Assignment 3: Javascript

100 points

Part 1 Javascript Basics (30 points)

Make sure that you've worked through the FIRST Javascript Tutorial prior to starting this part of the assignment.

1A. (5 points) Copy this code into the head of an HTML file (name the file username_js1.html using your own username), and run it to see what it does. Examine the code to develop an understanding of how it works.

<script type="text/javascript">
var d = new Date();
this_year = d.getFullYear();
this_month = d.getMonth() + 1; //getMonth returns month as 0-11 so we add 1
this_day = d.getDate();
str="Today is day " + this_day + " of month " + this_month + " in the year " + this_year;

1B (15 points). Edit the above code to calculate approximately how many months you have been alive (give or take some days) according to the date the program is run. Your output should looks very much like this:

1C. (10 points) Make sure that your name and the date you complete this part of the assignment are included in both the output and in comments in the javascript code.

Part 2 Javascript Prompts (20 points)

Make sure that you've worked through the SECOND Javascript Tutorial prior to starting this part of the section of the assignment.

2A. Copy the file you created in part one of this assignment and name the second file username_js2.html using your own username. Edit the second file so that it uses Javascript prompts to get the user's birth year, birth month, and birth day (in numeric format). Use that data to output the amount of months the user has been alive in exactly the same format as used in part one of this assignment. See screen shots for details. (10 points for proper prompts, 10 points for proper output)

Big Hint!

var birth_year = window.prompt("Please enter the year you were born in a four digit format.");



Part 3 Javascript Selection and Iteration (50 points)

3A. Re-examine the code in the Academic Progress Calculator example using "View Source". Note how it uses prompts, calculations, if-else if-else, and "for" to provide a useful service. Your mission for this part of the assignment is to come up with your own Javascript application that makes creative use of prompts, calculations, if-else if-else, and "for" to solve some problem, provide some service, or offer some entertainment. Your program idea must be approved by your instructor via email, or you will lose 50% of your points for this part. Your idea will need to be unique and different from all other students. Your program will need to use all the Javascript skills you have learned through the tutorials:

  • document.write that includes HTML tags (4 points)
  • variables (at least three) (4 points)
  • prompts (4 points)
  • calculations (at least one equation) (4 points)
  • if, else if, else (10 points)
  • a for loop (10 points)
  • creativity & ingenuity (9 points)
  • comment your code with your name and date and explanaitions of what is occuring in each step or set of steps (5 points)

Name your file username_js3.html

Debugging: If you have trouble getting Javascript to treat user input as numbers (ie. 12 + 12 = 1212?) use the parseInt function to change the text string to an integer (number). This trick was covered in the first tutorial in this example.


Upload your three javascript files to your A3 folder on garnet for grading. Change permissions as needed. Submit the A3 form on the Assignments page of our class Web site. Make sure to check your page and links from prior to submitting the assignment form indicating that you have finished.