To write your own JavaScript functions.
Web Programming book reading for THIS class
Web Programming book reading for NEXT class
Beginner’s Essential JavaScript Cheat Sheet
Questions from our last class or the readings or work between classes?
function
statement return
statementOur 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:
var
style makes it clear that a function is actually a
JavaScript variable CamelCase
makes the name immediately
distinguishable as a function name function
makes it clear that the variable type
is function
let
rather than var
to declare a
variable inside the function let
for variables that will not be used outside the functionlet
defines local variables that
are undefined
outside the scope of the
function1 <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
null
, sort of ...
null
using ==, you
get true
, but you can’t differentiate that from a
truly null
parameter null
using ===, you
get false
, which allows you to differentiate unsupplied parameters
from truly null
parameters typeof
:typeof
can be used as a function: if ( typeof( param ) == "undefined" ) { ... }
if ( typeof param == "undefined" ) { ... }
typeof
unambiguously
return true
for undefined parametersarguments.length
will tell you the number of parameters passed <head>
section
<head>
section?The return
statement
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
Goal: To write a program using a function and a table.
Procedure:
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. <head>
section of your
program.
document.writeln( RectangleArea( 3, 5 ) );
which should print 15 in your browser window.
console.log( RectangleArea( 3, 5 ) );
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.