Cs101 Lec35.pps
Cs101 Lec35.pps
Lecture 35
Mathematical Methods
(Web Development Lecture 12)
1
During the last lecture we discussed
Event handling
• We looked at the concept of event-driven
programs and event handlers
– What are they?
– What do they do?
– How do we benefit from them?
4
JavaScript’s Handling of Events
• Events handlers are placed in the BODY part of
a Web page as attributes in HTML tags
6
In-Line JavaScript Event Handling (2)
• Multiple JavaScript statements (separated by
semicolons) can be placed in that string, but all
have to fit in a single line; no newline
characters are allowed in that string
7
Usage Guideline
• For very short scripts, “all code in the tag”
works well
8
onFocus & onBlur
• onFocus executes the specified JavaScript
code when a window receives focus or when a
form element receives input focus
9
onLoad & onUnload
• onLoad executes the specified JavaScript code
when a new document is loaded into a window
10
A Note on Syntax (1)
• Mixed-case capitalization of event handlers
(e.g. onClick) is a convention (but not a
requirement) for JavaScript event handlers
defined in HTML code
11
A Note on Syntax (2)
• At times, you may wish to use event handlers in
JavaScript code enclosed in <SCRIPT>,
</SCRIPT> tags
12
Today’s Goal
(Mathematical Methods)
13
But first, let’s plot the sine function…
14
Problems & Solutions
• JavaScript doesn’t support drawing of graphics
16
17
So, all we want to do is plot the first
full-cycle of a sine function.
18
<HTML>
<HEAD>
<TITLE>Sine Function Plot</TITLE>
<SCRIPT>
function plotSine( ) {
…
}
…
</SCRIPT>
</HEAD>
<BODY onLoad="plotSine( )">
</BODY>
</HTML> 19
function plotSine( ) {
document.write(
"<H1 align = 'center'>sin(x)</H1>" ) ;
var rowE ;
document.write(
"<FONT face = 'courier' size = '-2'>" ) ;
document.write( "<BR></FONT>" ) ;
} 22
Drawing the x- and y-axes
23
function plotRow( rowN, ht, wd==) {0 )
if( rowE
var theta, rowE ; row[ rowE ] = "|" ;
var row = new Array(
elsewd ) ;
if( rowN == 0 )
for ( rowE=0; rowE <= wd; rowE = rowE + 1 ) {
row[ rowE ] = "-" ;
theta = 2 * Math.PI * else
rowE / wd ;
Or a tangent?
26
Today We Have Seen 3 New Elements
• Math.PI
– A property that gave us the value of Pi
• Math.round( )
– A method that rounded a number to its nearest
integer
• Math.sin( )
– A method that gave us the sine of an angle
random( )
30
sin( r ), cos( r ), tan( r )
Standard trigonometric functions
EXAMPLE
document.write( Math.cos( Math.PI / 4 ) )
0.7071067811865476
31
asin( x ), acos( x ), atan( x )
Standard inverse-trigonometric functions
EXAMPLE
document.write( Math.asin( 1 ) )
1.5707963267948965
32
sqrt( x ) pow( x, y )
Returns the Returns x
square root of raised to the
x power y
0.5 0.7071 2, 32
4294967296
33
exp( x ) log( x )
Returns Returns the
Math.E raised the natural
to the power x logarithm of x
1 2.718281 Math.E 1
34
round( x ) floor( x ) ceil( x )
Returns Returns Returns
integer largest integer smallest
nearest to x that is less integer that is
than or equal greater than
to x or equal to x
1.1 1 1.1 1 1.1 2
12.5 13 12.5 12 12.5 13
-13.9 -14 -13.9 -14 -13.9 -13
35
abs( x )
Returns the
absolute value
of x
1.1 1.1
-12.5 12.5
00
36
min( x, y ) max( x, y )
Returns the Returns the
smaller of x larger of x and
and y y
2, 4 2 2, 4 4
-12, -5 -12 -12, -5 -5
37
random( )
Returns a randomly-selected, floating-point
number between 0 and 1
EXAMPLE
document.write( Math.random( ) )
0.9601111965589273
38
random( ): Example
39
****
40
<HTML>
<HEAD>
<TITLE>Electronic Die</TITLE>
<SCRIPT>
function rollDie( ) { … }
</SCRIPT>
</HEAD>
<BODY>
<FORM … > … </FORM>
</BODY>
</HTML> 41
<FORM name="form1" method="post" action="">
</FORM>
42
function rollDie( ) { Asterisk
How? 44
Assignment #12
• Develop a Web page that displays an order
taking form
• It takes the number of items required for each
product, multiplies with the prices, sums them
up, adds the GST, and displays the total value
of the order
• Make sure to declare all variables that you use
in the main code as well as the functions
46
Next (the 13 ) Web Dev Lecture:
th
String Manipulation
• To become familiar with a few methods used
for manipulating strings
47