Website Development I

Course Info Class Notes Resources Student Websites

Class No. 16:

Writing Your Own JavaScript Functions

Today’s Goal

To write your own JavaScript functions.


Class Resources

Web Programming book reading for THIS class

Web Programming book reading for NEXT class

Beginner’s Essential JavaScript Cheat Sheet


Preambles

Questions from our last class or the readings or work between classes?


User-Defined Functions

Basic Structure

Important Note of Function Declaration Styles

Our book uses the following function declaration style:


source: Dean, Web Programming, p. 317

I prefer the following function declaration style:

     var DisplayHello = function() {
       let msg;
       msg = document.getElementById("message");
       msg.outerHTML = "<h1>Hello, world!</h1>";
     }

My reasons for using the var style:

Example

 1  <head>
 2    <script type="text/javascript">
 3      // return the area of a circle given its radius
 4      var CircleArea = function( radius ) {
 5        let area = Math.PI * radius * radius ;  // compute "pi r squared"
 6        return area.toFixed( 5 ) ;              // return result to 5 decimal places
 7      }
 8    </script>
 9  </head>
10 
11  <body>
12    <table class="CircleAreas" cellspacing="0" cellpadding="5" border="1">
13      <tr>
14        <th colspan="2">Circle Areas</th>
15      </tr>
16      <tr>
17        <th>Radius</th>
18        <th>Area</th>
19      </tr>
20      <tr>
21        <td>1</td>
22        <td>
23          <script>
24            document.writeln( CircleArea( 1 ) ) ;   // call function
25          </script>
26        </td>
27      </tr>
28      <tr>
29        <td>2</td>
30        <td>
31          <script>
32            document.writeln( CircleArea( 2 ) ) ;   // call function
33          </script>
34        </td>
35      </tr>
36    </table>
37  </body>
Circle Areas
Radius Area
1 3.14159
2 12.56637

adding a little CSS...

41  <style>
42    table.CircleAreas tr td:nth-child(1) {
43      text-align : center ;
44    }
45    table.CircleAreas tr td:nth-child(2) {
46      text-align : right ;
47    }
48  </style>
Circle Areas
Radius Area
1 3.14159
2 12.56637

Run the code yourself


Parameters

The return statement


Modification of the Above Example Using a JavaScript Loop

51  <table class="CircleAreas" cellspacing="0" cellpadding="5" border="1">
52    <tr>
53      <th colspan="2">Circle Areas</th>
54    </tr>
55    <tr>
56      <th>Radius</th>
57      <th>Area</th>
58    </tr>
59    <script>
60      var radius = 1 ;
61      for ( var k = 1 ; k <= 7 ; k++ ) {
62        document.writeln( "<tr>" ) ;
63        document.writeln( "<td>" + radius + "</td>" ) ;
64        document.writeln( "<td>" + CircleArea( radius, 2 ) + "</td>" ) ;
65        document.writeln( "</tr>" ) ;
66        radius = radius * 2 ;  // could also be written: radius *= 2 ;
67      }
68    </script>
69  </table>
Circle Areas
Radius Area
1
2
4
8
16
32
64


Today’s Exercise

Goal:  To write a program using a function and a table.

Procedure:

  1. Start fresh with a brand new page for this exercise.  You should still use the HTML5 template that we’ve been using throughout this course, but this will give us a fresh start.
  2. Write a function like CircleArea in the above examples, but that computes something other than the area of a circle.  For example, you might write a function that computes the area of a rectangle or a triangle.  If you don’t know the formulas for those shapes, just ask.
  3. Test your function by calling it with a single line of code just to make sure that it works as you want it to.
  4. You are now ready to create a table to display the output of multiple calls to your function.  Try using a loop like that in the last example above to generate the rows and columns of your table.
  5. Be sure to view your page in the browser through the web server using an address in the format:
  6. http://cssdweb.edu/username/filename.html


 


This is Class No. 16.  It was last modified on Saturday, October 21, 2023 at 4:38 PM.  Copyright © 2010-2024 by Jesse M. Heines.  All rights reserved, but may be freely copied or excerpted for educational purposes with credit to the author.