Professional Documents
Culture Documents
Lecture 35
Mathematical Methods (Web Development Lecture 12)
1
Alternatively, events can be captured in the HTML code, and then directed to a JavaScript function for an appropriate response 5
Usage Guideline
For very short scripts, all code in the tag works well The code in the HEAD portion is the right choice for developing larger JavaScript scripts
It makes the code easier to read It allows the reuse of a function for multiple event handlers
onBlur executes the specified JavaScript code when a window loses focus or a form element loses focus
10
11
In those cases you have to strictly follow the JavaScript rule for all event handler identifiers: they must all be typed in small case, e.g. onclick or onmouseover
12
13
14
Lets first take a look at the plot, and then we will discuss how to draw it
16
17
So, all we want to do is plot the first full-cycle of a sine function. And - to make things interesting - we would like to have control over the height and width of the plot
18
<HTML> <HEAD> <TITLE>Sine Function Plot</TITLE> <SCRIPT> function plotSine( ) { } </SCRIPT> </HEAD> <BODY onLoad="plotSine( )"> </BODY> 19 </HTML>
function plotSine( ) {
var ht, wd, rowN ; // rowN is the row number ht = 15 ; // height of the half cycle wd = 90 ; // width of the plot document.write( "<H1 align = 'center'>sin(x)</H1>" ) ; for( rowN = ht; rowN >= -ht; rowN = rowN - 1 ) { plotRow( rowN, ht, wd ) ; } }
20
function plotRow( rowN, ht, wd ) { var theta, rowE ; // rowE is the row element var row = new Array( wd ) ;
for ( rowE=0; rowE <= wd; rowE = rowE + 1 ) { theta = 2 * Math.PI * rowE / wd ; if( rowN == Math.round(ht * Math.sin( theta ))) row[ rowE ] = "*" ; else row[ rowE ] = " " ;
} writeRow ( row, wd ) ;
21
23
function plotRow( rowN, ht, wd ) { if( rowE == 0 ) var theta, rowE ; row[ rowE ] = "|" ; var row = new Array( wd ) ; else
if( rowN == 0 ) for ( rowE=0; rowE <= wd; rowE = rowE + 1 ) { row[ rowE ] = "-" ; theta = 2 * Math.PI * rowE / wd ; else if( rowN == Math.round(ht * Math.sin( theta ))) row[ rowE ] = "*" ; else row[ rowE ] = " " ; } writeRow ( row, wd ) ;
}
24
25
26
Math.round( )
A method that rounded a number to its nearest integer
Math.sin( )
A method that gave us the sine of an angle
27
Properties
Math.PI
Math.E
Math.LN2 Math.LN10 Math.LOG2E Math.LOG10E Math.SQRT2 Math.SQRT1_2
29
Methods
sin( r ) cos( r ) tan( r ) asin( x ) acos( x ) atan( x ) atan2( x, y ) sqrt( x ) pow( x, y ) exp( x ) log( x ) round( x ) floor( x ) ceil( x ) abs( x ) max( x, y ) max( x, y ) random( )
30
sqrt( x )
Returns the square root of x
pow( x, y )
Returns x raised to the power y
0.5 0.7071
2, 32 4294967296
33
exp( x )
Returns Math.E raised to the power x
log( x )
Returns the the natural logarithm of x
1 2.718281
Math.E 1
34
round( x )
Returns integer nearest to x
floor( x )
Returns largest integer that is less than or equal to x 1.1 1 12.5 12 -13.9 -14
ceil( x )
Returns smallest integer that is greater than or equal to x 1.1 2 12.5 13 -13.9 -13
35
abs( x )
Returns the absolute value of x
min( x, y )
Returns the smaller of x and y
max( x, y )
Returns the larger of x and y
2, 4 2 -12, -5 -12
2, 4 4 -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
Design a Web page that displays the result of the rolling of a 6-sided die on user command
39
****
40
<HTML> <HEAD> <TITLE>Electronic Die</TITLE> <SCRIPT> function rollDie( ) { } </SCRIPT> </HEAD> <BODY> <FORM > </FORM> </BODY> 41 </HTML>
<FORM name="form1" method="post" action=""> <INPUT type="submit" name="Submit" value="Roll Die" onMouseOver="rollDie( )"> <INPUT type="text" name="die" size="12"> </FORM>
42
Asterisk
dieN = Math.round( 6 * Math.random( ) ) ; for( dots = 2; dots <= dieN; dots = dots + 1 ) { dieDots = dieDots + "* " ; } document.form1.die.value = dieDots ;
43
The program shown here has a flaw: It will not come-up with all six numbers with a uniform probability.
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
Further information on this assignment will be provide on 45 the CS101 Web site
46
47