SlideShare une entreprise Scribd logo
1  sur  47
1
CS101 Introduction to Computing
Lecture 35Mathematical Methods
(Web Development Lecture 12)
2
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?
• We wrote simple programs to demonstrate the
capabilities of a few event handlers
3
What is Event Handling?
• Capturing events and responding to them
• The system sends events to the program and
the program responds to them as they arrive
• Events can include things a user does - like
clicking the mouse - or things that the system
itself does - like updating the clock. Today we
will exclusively focus on user-events
4
Event Driven Programs
• Programs that can capture and respond to
events are called ‘event-driven programs’
• JavaScript was specifically designed for writing
such programs
5
JavaScript’s Handling of Events
• Events handlers are placed in the BODY part of
a Web page as attributes in HTML tags
• Events can be captured and responded to
directly with JavaScript one-liners embedded in
HTML tags in the BODY portion
• Alternatively, events can be captured in the
HTML code, and then directed to a JavaScript
function for an appropriate response
6
In-Line JavaScript Event Handling (1)
• Event handlers are placed in the BODY portion
of a Web page as attributes of HTML tags
• The event handler attribute consists of 3 parts:
1.The identifier of the event handler
2.The equal sign
3.A string consisting of JavaScript statements
enclosed in double or single quotes
7
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
• Due to this limitation, sophisticated event
handling is not possible with in-line event
handling
8
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
9
onFocus & onBlur
• onFocus executes the specified JavaScript
code when a window receives focus or when a
form element receives input focus
• onBlur executes the specified JavaScript code
when a window loses focus or a form element
loses focus
10
onLoad & onUnload
• onLoad executes the specified JavaScript code
when a new document is loaded into a window
• onUnload executes the specified JavaScript
code when a user exits a document
11
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
12
A Note on Syntax (2)
• At times, you may wish to use event handlers in
JavaScript code enclosed in <SCRIPT>,
</SCRIPT> tags
• 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’
13
Today’s Goal
(Mathematical Methods)
• We will look at JavaScript’s Math object
• We will look at solutions for simple problems
using various methods of the Math object
14
But first, let’s plot the sine function…
15
Problems & Solutions
• JavaScript doesn’t support drawing of graphics
• However, crude graphics can be put together
with the help of various text characters or
tables
• One cannot write a character at a random
location on the screen using JavaScript
• Instead, the graph has to be drawn from top to
bottom, one row at a time – just like when
16
Let’s first take a look at the plot, and
then we will discuss how to draw it
17
18
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
19
<HTML>
<HEAD>
<TITLE>Sine Function Plot</TITLE>
<SCRIPT>
function plotSine( ) {
…
}
…
</SCRIPT>
</HEAD>
<BODY onLoad="plotSine( )">
</BODY>
</HTML>
20
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 ) ;
}
}
21
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 ] = "&nbsp;" ;
}
writeRow ( row, wd ) ;
}
22
function writeRow( row, wd ) {
var rowE ;
document.write(
"<FONT face = 'courier' size = '-2'>" ) ;
for( rowE = 0; rowE <= wd; rowE = rowE + 1 ) {
document.write ( row[ rowE ] ) ;
}
document.write( "<BR></FONT>" ) ;
}
23
Drawing the x- and y-axes
24
function plotRow( rowN, ht, wd ) {
var theta, rowE ;
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 ] = "&nbsp;" ;
}
writeRow ( row, wd ) ;
}
if( rowE == 0 )
row[ rowE ] = "|" ;
else
if( rowN == 0 )
row[ rowE ] = "-" ;
else
25
26
That was a sine wave that we looked at.
How about a cosine?
Or a tangent?
Or, even, the natural logarithm?
27
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
• All 3 belong to JavaScript’s Math object
28
Mathematical Functions in JavaScript
• In addition to the simple arithmetic operations
(e.g. +, *, etc.) JavaScript supports several
advanced mathematical operations as well
• Notationaly, these functions are accessed by
referring to various methods of the Math object
• Moreover, this object also contains several
useful mathematical constants as its properties
• This object has no use, but of a placeholder
29
Properties
Math.PI
Math.E
Math.LN2
Math.LN10
Math.LOG2E
Math.LOG10E
Math.SQRT2
Math.SQRT1_2
Note the
CAPITAL
lettering
of all
properties
30
Methods
random( )
sin( r )
cos( r )
tan( r )
asin( x )
acos( x )
atan( x )
atan2( x, y ) max( x, y )
max( x, y )
round( x )
floor( x )
ceil( x )
exp( x )
log( x ) abs( x )
sqrt( x )
pow( x, y )
31
sin( r ), cos( r ), tan( r )
Standard trigonometric functions
Returns the sine, cosine or tangent of ‘r’,
where ‘r’ is specified in radians
EXAMPLE
document.write( Math.cos( Math.PI / 4 ) )
0.7071067811865476
32
asin( x ), acos( x ), atan( x )
Standard inverse-trigonometric functions
Returns the arcsine, arccosine or arctangent of ‘r’
in radians
EXAMPLE
document.write( Math.asin( 1 ) )
1.5707963267948965
33
Returns the
square root of
x
sqrt( x )
0.5 → 0.7071
Returns x
raised to the
power y
pow( x, y )
2, 32 →
4294967296
34
Returns
Math.E raised
to the power x
exp( x )
1 → 2.718281
Returns the
the natural
logarithm of x
log( x )
Math.E → 1
35
ceil( x )
Returns
integer
nearest to x
Returns
largest integer
that is less
than or equal
to x
Returns
smallest
integer that is
greater than
or equal to x
floor( x )round( x )
1.1 → 1
12.5 → 13
-13.9 → -14
1.1 → 1
12.5 → 12
-13.9 → -14
1.1 → 2
12.5 → 13
-13.9 → -13
36
Returns the
absolute value
of x
abs( x )
1.1 → 1.1
-12.5 → 12.5
0 → 0
37
Returns the
smaller of x
and y
min( x, y )
2, 4 → 2
-12, -5 → -12
Returns the
larger of x and
y
max( x, y )
2, 4 → 4
-12, -5 → -5
38
random( )
Returns a randomly-selected, floating-point
number between 0 and 1
EXAMPLE
document.write( Math.random( ) )
0.9601111965589273
39
random( ): Example
Design a Web page that displays the
result of the rolling of a 6-sided die on
user command
40
* * * *
41
<HTML>
<HEAD>
<TITLE>Electronic Die</TITLE>
<SCRIPT>
function rollDie( ) { … }
</SCRIPT>
</HEAD>
<BODY>
<FORM … > … </FORM>
</BODY>
</HTML>
42
<FORM name="form1" method="post" action="">
<INPUT type="submit" name="Submit"
value="Roll Die" onMouseOver="rollDie( )">
<INPUT type="text" name="die" size="12">
</FORM>
43
function rollDie( ) {
var dieN, dieDots, dots ;
dieDots = "* " ;
dieN = Math.round( 6 * Math.random( ) ) ;
for( dots = 2; dots <= dieN; dots = dots + 1 ) {
dieDots = dieDots + "* " ;
}
document.form1.die.value = dieDots ;
}
Asterisk
44
The program shown here has a flaw: It will
not come-up with all six numbers with a
uniform probability.
In fact, it is biased against 6’es & for 1’s
How?
45
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
the CS101 Web site
46
During Today’s Lecture …
• We looked at the properties and methods of
JavaScript’s Math object
• We produced solutions for simple problems
using several methods of the Math object
47
Next (the 13th
) Web Dev Lecture:
String Manipulation
• To become familiar with a few methods used
for manipulating strings
• To become able to solve simple problems
involving strings

Contenu connexe

Tendances

JavaScript 2016 for C# Developers
JavaScript 2016 for C# DevelopersJavaScript 2016 for C# Developers
JavaScript 2016 for C# DevelopersRick Beerendonk
 
JavaScript Foundations Day1
JavaScript Foundations Day1JavaScript Foundations Day1
JavaScript Foundations Day1Troy Miles
 
미려한 UI/UX를 위한 여정
미려한 UI/UX를 위한 여정미려한 UI/UX를 위한 여정
미려한 UI/UX를 위한 여정SeungChul Kang
 
Oops lab manual2
Oops lab manual2Oops lab manual2
Oops lab manual2Mouna Guru
 
03 Geographic scripting in uDig - halfway between user and developer
03 Geographic scripting in uDig - halfway between user and developer03 Geographic scripting in uDig - halfway between user and developer
03 Geographic scripting in uDig - halfway between user and developerAndrea Antonello
 
Building a website in Haskell coming from Node.js
Building a website in Haskell coming from Node.jsBuilding a website in Haskell coming from Node.js
Building a website in Haskell coming from Node.jsNicolas Hery
 
TypeScript by Howard
TypeScript by HowardTypeScript by Howard
TypeScript by HowardLearningTech
 
Community-driven Language Design at TC39 on the JavaScript Pipeline Operator ...
Community-driven Language Design at TC39 on the JavaScript Pipeline Operator ...Community-driven Language Design at TC39 on the JavaScript Pipeline Operator ...
Community-driven Language Design at TC39 on the JavaScript Pipeline Operator ...Igalia
 
Type script by Howard
Type script by HowardType script by Howard
Type script by HowardLearningTech
 
Data structure new lab manual
Data structure  new lab manualData structure  new lab manual
Data structure new lab manualSANTOSH RATH
 
The Ring programming language version 1.6 book - Part 183 of 189
The Ring programming language version 1.6 book - Part 183 of 189The Ring programming language version 1.6 book - Part 183 of 189
The Ring programming language version 1.6 book - Part 183 of 189Mahmoud Samir Fayed
 
Dynamic C++ ACCU 2013
Dynamic C++ ACCU 2013Dynamic C++ ACCU 2013
Dynamic C++ ACCU 2013aleks-f
 

Tendances (19)

Lambda expressions in C++
Lambda expressions in C++Lambda expressions in C++
Lambda expressions in C++
 
JavaScript 2016 for C# Developers
JavaScript 2016 for C# DevelopersJavaScript 2016 for C# Developers
JavaScript 2016 for C# Developers
 
JavaScript Foundations Day1
JavaScript Foundations Day1JavaScript Foundations Day1
JavaScript Foundations Day1
 
미려한 UI/UX를 위한 여정
미려한 UI/UX를 위한 여정미려한 UI/UX를 위한 여정
미려한 UI/UX를 위한 여정
 
Oops lab manual2
Oops lab manual2Oops lab manual2
Oops lab manual2
 
03 Geographic scripting in uDig - halfway between user and developer
03 Geographic scripting in uDig - halfway between user and developer03 Geographic scripting in uDig - halfway between user and developer
03 Geographic scripting in uDig - halfway between user and developer
 
Struct examples
Struct examplesStruct examples
Struct examples
 
Cquestions
Cquestions Cquestions
Cquestions
 
Monad
MonadMonad
Monad
 
Building a website in Haskell coming from Node.js
Building a website in Haskell coming from Node.jsBuilding a website in Haskell coming from Node.js
Building a website in Haskell coming from Node.js
 
Hems
HemsHems
Hems
 
TypeScript by Howard
TypeScript by HowardTypeScript by Howard
TypeScript by Howard
 
Community-driven Language Design at TC39 on the JavaScript Pipeline Operator ...
Community-driven Language Design at TC39 on the JavaScript Pipeline Operator ...Community-driven Language Design at TC39 on the JavaScript Pipeline Operator ...
Community-driven Language Design at TC39 on the JavaScript Pipeline Operator ...
 
Type script by Howard
Type script by HowardType script by Howard
Type script by Howard
 
Advanced JavaScript
Advanced JavaScript Advanced JavaScript
Advanced JavaScript
 
C program
C programC program
C program
 
Data structure new lab manual
Data structure  new lab manualData structure  new lab manual
Data structure new lab manual
 
The Ring programming language version 1.6 book - Part 183 of 189
The Ring programming language version 1.6 book - Part 183 of 189The Ring programming language version 1.6 book - Part 183 of 189
The Ring programming language version 1.6 book - Part 183 of 189
 
Dynamic C++ ACCU 2013
Dynamic C++ ACCU 2013Dynamic C++ ACCU 2013
Dynamic C++ ACCU 2013
 

En vedette

ENG101- English Comprehension- Lecture 43
ENG101- English Comprehension- Lecture 43ENG101- English Comprehension- Lecture 43
ENG101- English Comprehension- Lecture 43Bilal Ahmed
 
MGT101 - Financial Accounting- Lecture 40
MGT101 - Financial Accounting- Lecture 40MGT101 - Financial Accounting- Lecture 40
MGT101 - Financial Accounting- Lecture 40Bilal Ahmed
 
CS101- Introduction to Computing- Lecture 44
CS101- Introduction to Computing- Lecture 44CS101- Introduction to Computing- Lecture 44
CS101- Introduction to Computing- Lecture 44Bilal Ahmed
 
MGT101 - Financial Accounting- Lecture 36
MGT101 - Financial Accounting- Lecture 36MGT101 - Financial Accounting- Lecture 36
MGT101 - Financial Accounting- Lecture 36Bilal Ahmed
 
MGT101 - Financial Accounting- Lecture 41
MGT101 - Financial Accounting- Lecture 41MGT101 - Financial Accounting- Lecture 41
MGT101 - Financial Accounting- Lecture 41Bilal Ahmed
 
CS201- Introduction to Programming- Lecture 03
CS201- Introduction to Programming- Lecture 03CS201- Introduction to Programming- Lecture 03
CS201- Introduction to Programming- Lecture 03Bilal Ahmed
 
CS101- Introduction to Computing- Lecture 39
CS101- Introduction to Computing- Lecture 39CS101- Introduction to Computing- Lecture 39
CS101- Introduction to Computing- Lecture 39Bilal Ahmed
 
CS101- Introduction to Computing- Lecture 29
CS101- Introduction to Computing- Lecture 29CS101- Introduction to Computing- Lecture 29
CS101- Introduction to Computing- Lecture 29Bilal Ahmed
 
MGT101 - Financial Accounting- Lecture 30
MGT101 - Financial Accounting- Lecture 30MGT101 - Financial Accounting- Lecture 30
MGT101 - Financial Accounting- Lecture 30Bilal Ahmed
 
ENG101- English Comprehension- Lecture 40
ENG101- English Comprehension- Lecture 40ENG101- English Comprehension- Lecture 40
ENG101- English Comprehension- Lecture 40Bilal Ahmed
 
CS201- Introduction to Programming- Lecture 06
CS201- Introduction to Programming- Lecture 06CS201- Introduction to Programming- Lecture 06
CS201- Introduction to Programming- Lecture 06Bilal Ahmed
 
CS201- Introduction to Programming- Lecture 12
CS201- Introduction to Programming- Lecture 12CS201- Introduction to Programming- Lecture 12
CS201- Introduction to Programming- Lecture 12Bilal Ahmed
 
MGT101 - Financial Accounting- Lecture 38
MGT101 - Financial Accounting- Lecture 38MGT101 - Financial Accounting- Lecture 38
MGT101 - Financial Accounting- Lecture 38Bilal Ahmed
 
MGT101 - Financial Accounting- Lecture 45
MGT101 - Financial Accounting- Lecture 45MGT101 - Financial Accounting- Lecture 45
MGT101 - Financial Accounting- Lecture 45Bilal Ahmed
 
ENG101- English Comprehension- Lecture 45
ENG101- English Comprehension- Lecture 45ENG101- English Comprehension- Lecture 45
ENG101- English Comprehension- Lecture 45Bilal Ahmed
 
CS201- Introduction to Programming- Lecture 22
CS201- Introduction to Programming- Lecture 22CS201- Introduction to Programming- Lecture 22
CS201- Introduction to Programming- Lecture 22Bilal Ahmed
 
CS101- Introduction to Computing- Lecture 32
CS101- Introduction to Computing- Lecture 32CS101- Introduction to Computing- Lecture 32
CS101- Introduction to Computing- Lecture 32Bilal Ahmed
 
CS201- Introduction to Programming- Lecture 35
CS201- Introduction to Programming- Lecture 35CS201- Introduction to Programming- Lecture 35
CS201- Introduction to Programming- Lecture 35Bilal Ahmed
 
ENG101- English Comprehension- Lecture 23
ENG101- English Comprehension- Lecture 23ENG101- English Comprehension- Lecture 23
ENG101- English Comprehension- Lecture 23Bilal Ahmed
 
CS101- Introduction to Computing- Lecture 43
CS101- Introduction to Computing- Lecture 43CS101- Introduction to Computing- Lecture 43
CS101- Introduction to Computing- Lecture 43Bilal Ahmed
 

En vedette (20)

ENG101- English Comprehension- Lecture 43
ENG101- English Comprehension- Lecture 43ENG101- English Comprehension- Lecture 43
ENG101- English Comprehension- Lecture 43
 
MGT101 - Financial Accounting- Lecture 40
MGT101 - Financial Accounting- Lecture 40MGT101 - Financial Accounting- Lecture 40
MGT101 - Financial Accounting- Lecture 40
 
CS101- Introduction to Computing- Lecture 44
CS101- Introduction to Computing- Lecture 44CS101- Introduction to Computing- Lecture 44
CS101- Introduction to Computing- Lecture 44
 
MGT101 - Financial Accounting- Lecture 36
MGT101 - Financial Accounting- Lecture 36MGT101 - Financial Accounting- Lecture 36
MGT101 - Financial Accounting- Lecture 36
 
MGT101 - Financial Accounting- Lecture 41
MGT101 - Financial Accounting- Lecture 41MGT101 - Financial Accounting- Lecture 41
MGT101 - Financial Accounting- Lecture 41
 
CS201- Introduction to Programming- Lecture 03
CS201- Introduction to Programming- Lecture 03CS201- Introduction to Programming- Lecture 03
CS201- Introduction to Programming- Lecture 03
 
CS101- Introduction to Computing- Lecture 39
CS101- Introduction to Computing- Lecture 39CS101- Introduction to Computing- Lecture 39
CS101- Introduction to Computing- Lecture 39
 
CS101- Introduction to Computing- Lecture 29
CS101- Introduction to Computing- Lecture 29CS101- Introduction to Computing- Lecture 29
CS101- Introduction to Computing- Lecture 29
 
MGT101 - Financial Accounting- Lecture 30
MGT101 - Financial Accounting- Lecture 30MGT101 - Financial Accounting- Lecture 30
MGT101 - Financial Accounting- Lecture 30
 
ENG101- English Comprehension- Lecture 40
ENG101- English Comprehension- Lecture 40ENG101- English Comprehension- Lecture 40
ENG101- English Comprehension- Lecture 40
 
CS201- Introduction to Programming- Lecture 06
CS201- Introduction to Programming- Lecture 06CS201- Introduction to Programming- Lecture 06
CS201- Introduction to Programming- Lecture 06
 
CS201- Introduction to Programming- Lecture 12
CS201- Introduction to Programming- Lecture 12CS201- Introduction to Programming- Lecture 12
CS201- Introduction to Programming- Lecture 12
 
MGT101 - Financial Accounting- Lecture 38
MGT101 - Financial Accounting- Lecture 38MGT101 - Financial Accounting- Lecture 38
MGT101 - Financial Accounting- Lecture 38
 
MGT101 - Financial Accounting- Lecture 45
MGT101 - Financial Accounting- Lecture 45MGT101 - Financial Accounting- Lecture 45
MGT101 - Financial Accounting- Lecture 45
 
ENG101- English Comprehension- Lecture 45
ENG101- English Comprehension- Lecture 45ENG101- English Comprehension- Lecture 45
ENG101- English Comprehension- Lecture 45
 
CS201- Introduction to Programming- Lecture 22
CS201- Introduction to Programming- Lecture 22CS201- Introduction to Programming- Lecture 22
CS201- Introduction to Programming- Lecture 22
 
CS101- Introduction to Computing- Lecture 32
CS101- Introduction to Computing- Lecture 32CS101- Introduction to Computing- Lecture 32
CS101- Introduction to Computing- Lecture 32
 
CS201- Introduction to Programming- Lecture 35
CS201- Introduction to Programming- Lecture 35CS201- Introduction to Programming- Lecture 35
CS201- Introduction to Programming- Lecture 35
 
ENG101- English Comprehension- Lecture 23
ENG101- English Comprehension- Lecture 23ENG101- English Comprehension- Lecture 23
ENG101- English Comprehension- Lecture 23
 
CS101- Introduction to Computing- Lecture 43
CS101- Introduction to Computing- Lecture 43CS101- Introduction to Computing- Lecture 43
CS101- Introduction to Computing- Lecture 43
 

Similaire à CS101- Introduction to Computing- Lecture 35

The Art Of Readable Code
The Art Of Readable CodeThe Art Of Readable Code
The Art Of Readable CodeBaidu, Inc.
 
Intro to Javascript
Intro to JavascriptIntro to Javascript
Intro to JavascriptAnjan Banda
 
Emerging Languages: A Tour of the Horizon
Emerging Languages: A Tour of the HorizonEmerging Languages: A Tour of the Horizon
Emerging Languages: A Tour of the HorizonAlex Payne
 
Funtional Reactive Programming with Examples in Scala + GWT
Funtional Reactive Programming with Examples in Scala + GWTFuntional Reactive Programming with Examples in Scala + GWT
Funtional Reactive Programming with Examples in Scala + GWTVasil Remeniuk
 
Introduction to Coding
Introduction to CodingIntroduction to Coding
Introduction to CodingFabio506452
 
elm-d3 @ NYC D3.js Meetup (30 June, 2014)
elm-d3 @ NYC D3.js Meetup (30 June, 2014)elm-d3 @ NYC D3.js Meetup (30 June, 2014)
elm-d3 @ NYC D3.js Meetup (30 June, 2014)Spiros
 
Options and trade offs for parallelism and concurrency in Modern C++
Options and trade offs for parallelism and concurrency in Modern C++Options and trade offs for parallelism and concurrency in Modern C++
Options and trade offs for parallelism and concurrency in Modern C++Satalia
 
Stratosphere Intro (Java and Scala Interface)
Stratosphere Intro (Java and Scala Interface)Stratosphere Intro (Java and Scala Interface)
Stratosphere Intro (Java and Scala Interface)Robert Metzger
 
javascript Event Handling and introduction to event.ppt
javascript Event Handling and introduction to event.pptjavascript Event Handling and introduction to event.ppt
javascript Event Handling and introduction to event.pptLalith86
 
CONFidence 2015: DTrace + OSX = Fun - Andrzej Dyjak
CONFidence 2015: DTrace + OSX = Fun - Andrzej Dyjak   CONFidence 2015: DTrace + OSX = Fun - Andrzej Dyjak
CONFidence 2015: DTrace + OSX = Fun - Andrzej Dyjak PROIDEA
 
Lab Practices and Works Documentation / Report on Computer Graphics
Lab Practices and Works Documentation / Report on Computer GraphicsLab Practices and Works Documentation / Report on Computer Graphics
Lab Practices and Works Documentation / Report on Computer GraphicsRup Chowdhury
 

Similaire à CS101- Introduction to Computing- Lecture 35 (20)

The Art Of Readable Code
The Art Of Readable CodeThe Art Of Readable Code
The Art Of Readable Code
 
Intro to Javascript
Intro to JavascriptIntro to Javascript
Intro to Javascript
 
Emerging Languages: A Tour of the Horizon
Emerging Languages: A Tour of the HorizonEmerging Languages: A Tour of the Horizon
Emerging Languages: A Tour of the Horizon
 
RxJava on Android
RxJava on AndroidRxJava on Android
RxJava on Android
 
Cpp tutorial
Cpp tutorialCpp tutorial
Cpp tutorial
 
Funtional Reactive Programming with Examples in Scala + GWT
Funtional Reactive Programming with Examples in Scala + GWTFuntional Reactive Programming with Examples in Scala + GWT
Funtional Reactive Programming with Examples in Scala + GWT
 
Introduction to Coding
Introduction to CodingIntroduction to Coding
Introduction to Coding
 
elm-d3 @ NYC D3.js Meetup (30 June, 2014)
elm-d3 @ NYC D3.js Meetup (30 June, 2014)elm-d3 @ NYC D3.js Meetup (30 June, 2014)
elm-d3 @ NYC D3.js Meetup (30 June, 2014)
 
Options and trade offs for parallelism and concurrency in Modern C++
Options and trade offs for parallelism and concurrency in Modern C++Options and trade offs for parallelism and concurrency in Modern C++
Options and trade offs for parallelism and concurrency in Modern C++
 
Stratosphere Intro (Java and Scala Interface)
Stratosphere Intro (Java and Scala Interface)Stratosphere Intro (Java and Scala Interface)
Stratosphere Intro (Java and Scala Interface)
 
CppTutorial.ppt
CppTutorial.pptCppTutorial.ppt
CppTutorial.ppt
 
Client sidescripting javascript
Client sidescripting javascriptClient sidescripting javascript
Client sidescripting javascript
 
cb streams - gavin pickin
cb streams - gavin pickincb streams - gavin pickin
cb streams - gavin pickin
 
Lecture 12 os
Lecture 12 osLecture 12 os
Lecture 12 os
 
Day 1
Day 1Day 1
Day 1
 
JavaScript for real men
JavaScript for real menJavaScript for real men
JavaScript for real men
 
javascript Event Handling and introduction to event.ppt
javascript Event Handling and introduction to event.pptjavascript Event Handling and introduction to event.ppt
javascript Event Handling and introduction to event.ppt
 
CONFidence 2015: DTrace + OSX = Fun - Andrzej Dyjak
CONFidence 2015: DTrace + OSX = Fun - Andrzej Dyjak   CONFidence 2015: DTrace + OSX = Fun - Andrzej Dyjak
CONFidence 2015: DTrace + OSX = Fun - Andrzej Dyjak
 
Lambdas puzzler - Peter Lawrey
Lambdas puzzler - Peter LawreyLambdas puzzler - Peter Lawrey
Lambdas puzzler - Peter Lawrey
 
Lab Practices and Works Documentation / Report on Computer Graphics
Lab Practices and Works Documentation / Report on Computer GraphicsLab Practices and Works Documentation / Report on Computer Graphics
Lab Practices and Works Documentation / Report on Computer Graphics
 

Plus de Bilal Ahmed

CS201- Introduction to Programming- Lecture 45
CS201- Introduction to Programming- Lecture 45CS201- Introduction to Programming- Lecture 45
CS201- Introduction to Programming- Lecture 45Bilal Ahmed
 
CS201- Introduction to Programming- Lecture 44
CS201- Introduction to Programming- Lecture 44CS201- Introduction to Programming- Lecture 44
CS201- Introduction to Programming- Lecture 44Bilal Ahmed
 
CS201- Introduction to Programming- Lecture 43
CS201- Introduction to Programming- Lecture 43CS201- Introduction to Programming- Lecture 43
CS201- Introduction to Programming- Lecture 43Bilal Ahmed
 
CS201- Introduction to Programming- Lecture 42
CS201- Introduction to Programming- Lecture 42CS201- Introduction to Programming- Lecture 42
CS201- Introduction to Programming- Lecture 42Bilal Ahmed
 
CS201- Introduction to Programming- Lecture 41
CS201- Introduction to Programming- Lecture 41CS201- Introduction to Programming- Lecture 41
CS201- Introduction to Programming- Lecture 41Bilal Ahmed
 
CS201- Introduction to Programming- Lecture 40
CS201- Introduction to Programming- Lecture 40CS201- Introduction to Programming- Lecture 40
CS201- Introduction to Programming- Lecture 40Bilal Ahmed
 
CS201- Introduction to Programming- Lecture 39
CS201- Introduction to Programming- Lecture 39CS201- Introduction to Programming- Lecture 39
CS201- Introduction to Programming- Lecture 39Bilal Ahmed
 
CS201- Introduction to Programming- Lecture 38
CS201- Introduction to Programming- Lecture 38CS201- Introduction to Programming- Lecture 38
CS201- Introduction to Programming- Lecture 38Bilal Ahmed
 
CS201- Introduction to Programming- Lecture 37
CS201- Introduction to Programming- Lecture 37CS201- Introduction to Programming- Lecture 37
CS201- Introduction to Programming- Lecture 37Bilal Ahmed
 
CS201- Introduction to Programming- Lecture 36
CS201- Introduction to Programming- Lecture 36CS201- Introduction to Programming- Lecture 36
CS201- Introduction to Programming- Lecture 36Bilal Ahmed
 
CS201- Introduction to Programming- Lecture 34
CS201- Introduction to Programming- Lecture 34CS201- Introduction to Programming- Lecture 34
CS201- Introduction to Programming- Lecture 34Bilal Ahmed
 
CS201- Introduction to Programming- Lecture 33
CS201- Introduction to Programming- Lecture 33CS201- Introduction to Programming- Lecture 33
CS201- Introduction to Programming- Lecture 33Bilal Ahmed
 
CS201- Introduction to Programming- Lecture 32
CS201- Introduction to Programming- Lecture 32CS201- Introduction to Programming- Lecture 32
CS201- Introduction to Programming- Lecture 32Bilal Ahmed
 
CS201- Introduction to Programming- Lecture 31
CS201- Introduction to Programming- Lecture 31CS201- Introduction to Programming- Lecture 31
CS201- Introduction to Programming- Lecture 31Bilal Ahmed
 
CS201- Introduction to Programming- Lecture 30
CS201- Introduction to Programming- Lecture 30CS201- Introduction to Programming- Lecture 30
CS201- Introduction to Programming- Lecture 30Bilal Ahmed
 
CS201- Introduction to Programming- Lecture 29
CS201- Introduction to Programming- Lecture 29CS201- Introduction to Programming- Lecture 29
CS201- Introduction to Programming- Lecture 29Bilal Ahmed
 
CS201- Introduction to Programming- Lecture 28
CS201- Introduction to Programming- Lecture 28CS201- Introduction to Programming- Lecture 28
CS201- Introduction to Programming- Lecture 28Bilal Ahmed
 
CS201- Introduction to Programming- Lecture 27
CS201- Introduction to Programming- Lecture 27CS201- Introduction to Programming- Lecture 27
CS201- Introduction to Programming- Lecture 27Bilal Ahmed
 
CS201- Introduction to Programming- Lecture 26
CS201- Introduction to Programming- Lecture 26CS201- Introduction to Programming- Lecture 26
CS201- Introduction to Programming- Lecture 26Bilal Ahmed
 
CS201- Introduction to Programming- Lecture 25
CS201- Introduction to Programming- Lecture 25CS201- Introduction to Programming- Lecture 25
CS201- Introduction to Programming- Lecture 25Bilal Ahmed
 

Plus de Bilal Ahmed (20)

CS201- Introduction to Programming- Lecture 45
CS201- Introduction to Programming- Lecture 45CS201- Introduction to Programming- Lecture 45
CS201- Introduction to Programming- Lecture 45
 
CS201- Introduction to Programming- Lecture 44
CS201- Introduction to Programming- Lecture 44CS201- Introduction to Programming- Lecture 44
CS201- Introduction to Programming- Lecture 44
 
CS201- Introduction to Programming- Lecture 43
CS201- Introduction to Programming- Lecture 43CS201- Introduction to Programming- Lecture 43
CS201- Introduction to Programming- Lecture 43
 
CS201- Introduction to Programming- Lecture 42
CS201- Introduction to Programming- Lecture 42CS201- Introduction to Programming- Lecture 42
CS201- Introduction to Programming- Lecture 42
 
CS201- Introduction to Programming- Lecture 41
CS201- Introduction to Programming- Lecture 41CS201- Introduction to Programming- Lecture 41
CS201- Introduction to Programming- Lecture 41
 
CS201- Introduction to Programming- Lecture 40
CS201- Introduction to Programming- Lecture 40CS201- Introduction to Programming- Lecture 40
CS201- Introduction to Programming- Lecture 40
 
CS201- Introduction to Programming- Lecture 39
CS201- Introduction to Programming- Lecture 39CS201- Introduction to Programming- Lecture 39
CS201- Introduction to Programming- Lecture 39
 
CS201- Introduction to Programming- Lecture 38
CS201- Introduction to Programming- Lecture 38CS201- Introduction to Programming- Lecture 38
CS201- Introduction to Programming- Lecture 38
 
CS201- Introduction to Programming- Lecture 37
CS201- Introduction to Programming- Lecture 37CS201- Introduction to Programming- Lecture 37
CS201- Introduction to Programming- Lecture 37
 
CS201- Introduction to Programming- Lecture 36
CS201- Introduction to Programming- Lecture 36CS201- Introduction to Programming- Lecture 36
CS201- Introduction to Programming- Lecture 36
 
CS201- Introduction to Programming- Lecture 34
CS201- Introduction to Programming- Lecture 34CS201- Introduction to Programming- Lecture 34
CS201- Introduction to Programming- Lecture 34
 
CS201- Introduction to Programming- Lecture 33
CS201- Introduction to Programming- Lecture 33CS201- Introduction to Programming- Lecture 33
CS201- Introduction to Programming- Lecture 33
 
CS201- Introduction to Programming- Lecture 32
CS201- Introduction to Programming- Lecture 32CS201- Introduction to Programming- Lecture 32
CS201- Introduction to Programming- Lecture 32
 
CS201- Introduction to Programming- Lecture 31
CS201- Introduction to Programming- Lecture 31CS201- Introduction to Programming- Lecture 31
CS201- Introduction to Programming- Lecture 31
 
CS201- Introduction to Programming- Lecture 30
CS201- Introduction to Programming- Lecture 30CS201- Introduction to Programming- Lecture 30
CS201- Introduction to Programming- Lecture 30
 
CS201- Introduction to Programming- Lecture 29
CS201- Introduction to Programming- Lecture 29CS201- Introduction to Programming- Lecture 29
CS201- Introduction to Programming- Lecture 29
 
CS201- Introduction to Programming- Lecture 28
CS201- Introduction to Programming- Lecture 28CS201- Introduction to Programming- Lecture 28
CS201- Introduction to Programming- Lecture 28
 
CS201- Introduction to Programming- Lecture 27
CS201- Introduction to Programming- Lecture 27CS201- Introduction to Programming- Lecture 27
CS201- Introduction to Programming- Lecture 27
 
CS201- Introduction to Programming- Lecture 26
CS201- Introduction to Programming- Lecture 26CS201- Introduction to Programming- Lecture 26
CS201- Introduction to Programming- Lecture 26
 
CS201- Introduction to Programming- Lecture 25
CS201- Introduction to Programming- Lecture 25CS201- Introduction to Programming- Lecture 25
CS201- Introduction to Programming- Lecture 25
 

Dernier

BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...Sapna Thakur
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingTechSoup
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphThiyagu K
 
social pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajansocial pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajanpragatimahajan3
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdfQucHHunhnh
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformChameera Dedduwage
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactdawncurless
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeThiyagu K
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDThiyagu K
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Krashi Coaching
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxSayali Powar
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxiammrhaywood
 
Russian Call Girls in Andheri Airport Mumbai WhatsApp 9167673311 💞 Full Nigh...
Russian Call Girls in Andheri Airport Mumbai WhatsApp  9167673311 💞 Full Nigh...Russian Call Girls in Andheri Airport Mumbai WhatsApp  9167673311 💞 Full Nigh...
Russian Call Girls in Andheri Airport Mumbai WhatsApp 9167673311 💞 Full Nigh...Pooja Nehwal
 
The byproduct of sericulture in different industries.pptx
The byproduct of sericulture in different industries.pptxThe byproduct of sericulture in different industries.pptx
The byproduct of sericulture in different industries.pptxShobhayan Kirtania
 
9548086042 for call girls in Indira Nagar with room service
9548086042  for call girls in Indira Nagar  with room service9548086042  for call girls in Indira Nagar  with room service
9548086042 for call girls in Indira Nagar with room servicediscovermytutordmt
 
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...fonyou31
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfchloefrazer622
 

Dernier (20)

BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot Graph
 
social pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajansocial pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajan
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy Reform
 
Advance Mobile Application Development class 07
Advance Mobile Application Development class 07Advance Mobile Application Development class 07
Advance Mobile Application Development class 07
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SD
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
Russian Call Girls in Andheri Airport Mumbai WhatsApp 9167673311 💞 Full Nigh...
Russian Call Girls in Andheri Airport Mumbai WhatsApp  9167673311 💞 Full Nigh...Russian Call Girls in Andheri Airport Mumbai WhatsApp  9167673311 💞 Full Nigh...
Russian Call Girls in Andheri Airport Mumbai WhatsApp 9167673311 💞 Full Nigh...
 
The byproduct of sericulture in different industries.pptx
The byproduct of sericulture in different industries.pptxThe byproduct of sericulture in different industries.pptx
The byproduct of sericulture in different industries.pptx
 
9548086042 for call girls in Indira Nagar with room service
9548086042  for call girls in Indira Nagar  with room service9548086042  for call girls in Indira Nagar  with room service
9548086042 for call girls in Indira Nagar with room service
 
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
 
Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1
 
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptxINDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdf
 

CS101- Introduction to Computing- Lecture 35

  • 1. 1 CS101 Introduction to Computing Lecture 35Mathematical Methods (Web Development Lecture 12)
  • 2. 2 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? • We wrote simple programs to demonstrate the capabilities of a few event handlers
  • 3. 3 What is Event Handling? • Capturing events and responding to them • The system sends events to the program and the program responds to them as they arrive • Events can include things a user does - like clicking the mouse - or things that the system itself does - like updating the clock. Today we will exclusively focus on user-events
  • 4. 4 Event Driven Programs • Programs that can capture and respond to events are called ‘event-driven programs’ • JavaScript was specifically designed for writing such programs
  • 5. 5 JavaScript’s Handling of Events • Events handlers are placed in the BODY part of a Web page as attributes in HTML tags • Events can be captured and responded to directly with JavaScript one-liners embedded in HTML tags in the BODY portion • Alternatively, events can be captured in the HTML code, and then directed to a JavaScript function for an appropriate response
  • 6. 6 In-Line JavaScript Event Handling (1) • Event handlers are placed in the BODY portion of a Web page as attributes of HTML tags • The event handler attribute consists of 3 parts: 1.The identifier of the event handler 2.The equal sign 3.A string consisting of JavaScript statements enclosed in double or single quotes
  • 7. 7 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 • Due to this limitation, sophisticated event handling is not possible with in-line event handling
  • 8. 8 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
  • 9. 9 onFocus & onBlur • onFocus executes the specified JavaScript code when a window receives focus or when a form element receives input focus • onBlur executes the specified JavaScript code when a window loses focus or a form element loses focus
  • 10. 10 onLoad & onUnload • onLoad executes the specified JavaScript code when a new document is loaded into a window • onUnload executes the specified JavaScript code when a user exits a document
  • 11. 11 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
  • 12. 12 A Note on Syntax (2) • At times, you may wish to use event handlers in JavaScript code enclosed in <SCRIPT>, </SCRIPT> tags • 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’
  • 13. 13 Today’s Goal (Mathematical Methods) • We will look at JavaScript’s Math object • We will look at solutions for simple problems using various methods of the Math object
  • 14. 14 But first, let’s plot the sine function…
  • 15. 15 Problems & Solutions • JavaScript doesn’t support drawing of graphics • However, crude graphics can be put together with the help of various text characters or tables • One cannot write a character at a random location on the screen using JavaScript • Instead, the graph has to be drawn from top to bottom, one row at a time – just like when
  • 16. 16 Let’s first take a look at the plot, and then we will discuss how to draw it
  • 17. 17
  • 18. 18 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
  • 19. 19 <HTML> <HEAD> <TITLE>Sine Function Plot</TITLE> <SCRIPT> function plotSine( ) { … } … </SCRIPT> </HEAD> <BODY onLoad="plotSine( )"> </BODY> </HTML>
  • 20. 20 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 ) ; } }
  • 21. 21 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 ] = "&nbsp;" ; } writeRow ( row, wd ) ; }
  • 22. 22 function writeRow( row, wd ) { var rowE ; document.write( "<FONT face = 'courier' size = '-2'>" ) ; for( rowE = 0; rowE <= wd; rowE = rowE + 1 ) { document.write ( row[ rowE ] ) ; } document.write( "<BR></FONT>" ) ; }
  • 23. 23 Drawing the x- and y-axes
  • 24. 24 function plotRow( rowN, ht, wd ) { var theta, rowE ; 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 ] = "&nbsp;" ; } writeRow ( row, wd ) ; } if( rowE == 0 ) row[ rowE ] = "|" ; else if( rowN == 0 ) row[ rowE ] = "-" ; else
  • 25. 25
  • 26. 26 That was a sine wave that we looked at. How about a cosine? Or a tangent? Or, even, the natural logarithm?
  • 27. 27 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 • All 3 belong to JavaScript’s Math object
  • 28. 28 Mathematical Functions in JavaScript • In addition to the simple arithmetic operations (e.g. +, *, etc.) JavaScript supports several advanced mathematical operations as well • Notationaly, these functions are accessed by referring to various methods of the Math object • Moreover, this object also contains several useful mathematical constants as its properties • This object has no use, but of a placeholder
  • 30. 30 Methods random( ) sin( r ) cos( r ) tan( r ) asin( x ) acos( x ) atan( x ) atan2( x, y ) max( x, y ) max( x, y ) round( x ) floor( x ) ceil( x ) exp( x ) log( x ) abs( x ) sqrt( x ) pow( x, y )
  • 31. 31 sin( r ), cos( r ), tan( r ) Standard trigonometric functions Returns the sine, cosine or tangent of ‘r’, where ‘r’ is specified in radians EXAMPLE document.write( Math.cos( Math.PI / 4 ) ) 0.7071067811865476
  • 32. 32 asin( x ), acos( x ), atan( x ) Standard inverse-trigonometric functions Returns the arcsine, arccosine or arctangent of ‘r’ in radians EXAMPLE document.write( Math.asin( 1 ) ) 1.5707963267948965
  • 33. 33 Returns the square root of x sqrt( x ) 0.5 → 0.7071 Returns x raised to the power y pow( x, y ) 2, 32 → 4294967296
  • 34. 34 Returns Math.E raised to the power x exp( x ) 1 → 2.718281 Returns the the natural logarithm of x log( x ) Math.E → 1
  • 35. 35 ceil( x ) Returns integer nearest to x Returns largest integer that is less than or equal to x Returns smallest integer that is greater than or equal to x floor( x )round( x ) 1.1 → 1 12.5 → 13 -13.9 → -14 1.1 → 1 12.5 → 12 -13.9 → -14 1.1 → 2 12.5 → 13 -13.9 → -13
  • 36. 36 Returns the absolute value of x abs( x ) 1.1 → 1.1 -12.5 → 12.5 0 → 0
  • 37. 37 Returns the smaller of x and y min( x, y ) 2, 4 → 2 -12, -5 → -12 Returns the larger of x and y max( x, y ) 2, 4 → 4 -12, -5 → -5
  • 38. 38 random( ) Returns a randomly-selected, floating-point number between 0 and 1 EXAMPLE document.write( Math.random( ) ) 0.9601111965589273
  • 39. 39 random( ): Example Design a Web page that displays the result of the rolling of a 6-sided die on user command
  • 40. 40 * * * *
  • 41. 41 <HTML> <HEAD> <TITLE>Electronic Die</TITLE> <SCRIPT> function rollDie( ) { … } </SCRIPT> </HEAD> <BODY> <FORM … > … </FORM> </BODY> </HTML>
  • 42. 42 <FORM name="form1" method="post" action=""> <INPUT type="submit" name="Submit" value="Roll Die" onMouseOver="rollDie( )"> <INPUT type="text" name="die" size="12"> </FORM>
  • 43. 43 function rollDie( ) { var dieN, dieDots, dots ; dieDots = "* " ; dieN = Math.round( 6 * Math.random( ) ) ; for( dots = 2; dots <= dieN; dots = dots + 1 ) { dieDots = dieDots + "* " ; } document.form1.die.value = dieDots ; } Asterisk
  • 44. 44 The program shown here has a flaw: It will not come-up with all six numbers with a uniform probability. In fact, it is biased against 6’es & for 1’s How?
  • 45. 45 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 the CS101 Web site
  • 46. 46 During Today’s Lecture … • We looked at the properties and methods of JavaScript’s Math object • We produced solutions for simple problems using several methods of the Math object
  • 47. 47 Next (the 13th ) Web Dev Lecture: String Manipulation • To become familiar with a few methods used for manipulating strings • To become able to solve simple problems involving strings