BuiltWithNOF
Form Letter

Cut and paste the following code into notepad. Save it as FormLetter1.html to the Desktop. Now run it by double-clicking the icon on your Desktop.    Study the code. Your task is to change the program so that it produces a Form letter, where you ask for the name, age (these 2 are done), city, school name, and year in school using window.prompt. Your program will then print the following, assuming you entered  Joe, 21,  Tacoma, Stadium High School, and sophomore as your inputs:

Joe lives in Tacoma and is 21 years old,  attends Stadium High School and is a sophomore.  Next year Joe will be 22.

Note the red words are from the input. Note also that 22 has been calculated by adding 1 to the age.  Run it with different inputs to get different letters. Turn in your code and 2 example letters next time. Try to add new inputs.

 

<html>
   <head>
     <title>A Form Letter Program</title>

     <script type = "text/javascript">
         <!--
         var nameVal,  // first string entered by user
           ageVal,  // second string entered by user

           schoolName,  // you will need to get and
           schoolYear,  // use these next 3 variables
           city;

         // read in the name from the user as a string
         nameVal =
           window.prompt( "Enter the name", "0" );

         // read in the name from user as a string
       ageVal =
           window.prompt( "Enter age", "0" );

         // convert age from a string to an integer
         //age = parseInt( age );
      

         // display the results
         document.writeln( "<h1>" + nameVal + " is " + ageVal + " years old." + "</h1>" );
         // -->
     </script>

   </head>
   <body>
     <p>Click Refresh (or Reload) to run the script again</p>
   </body>
</html>

CHALLENGE: See if you can make a Christmas Card program that will automatically ask for things that will change between folks you are writing to, and then print out a “personalized”  Christmas Card.

[Home] [Syllabus] [Sessions] [Writing Code] [Help] [Setup] [Instructor]