Title: JavaScript I
1Lecture 2
- JavaScript I
- Introduction to Scripting
22.1 Introduction to Scripting
- JavaScript scripting language
- Enhances functionality and appearance
- Client-side scripting
- Makes pages more dynamic and interactive
- Foundation for complex server-side scripting
- Program development
- Program control
32.2 Simple Program Printing a Line of Text in a
Web Page
- Inline scripting
- Written in the ltbodygt of a document
- ltscriptgt tag
- Indicate that the text is part of a script
- type attribute
- Specifies the type of file and the scripting
language use - writeln method
- Write a line in the document
- Escape character ( \ )
- Indicates special character is used in the
string - alert method
- Dialog box
4welcome.html(1 of 1)
5welcome2.html(1 of 1)
6welcome3.html1 of 1
7welcome4.html1 of 1
8(No Transcript)
92.2 Simple Program Printing a Line of Text in a
Web Page
102.3 Dynamic Welcome Page
- A script can adapt the content based on input
from the user or other variables
11welcome5.html(1 of 2)
12welcome5.html(2 of 2)
132.3.1 Dynamic Welcome Page
Fig. 2.7 Prompt dialog displayed by the window
objects prompt method.
142.3.2 Adding Integers
- Prompt user for two integers and calculate the
sum - NaN (not a number)
- parseInt
- Converts its string argument to an integer
15Addition.html(1 of 2)
16Addition.html(2 of 2)
17(No Transcript)
182.4 Memory Concepts
- Variable names correspond to locations in the
computers memory - Every variable has a name, a type, and a value
- Read value from a memory location
- nondestructive
192.4 Memory Concepts
Fig. 2.9 Memory location showing the name and
value of variable number1.
202.4 Memory Concepts
Fig. 2.10 Memory locations after values for
variables number1 and number2 have been input.
212.4 Memory Concepts
Fig. 2.11 Memory locations after calculating the
sum of number1 and number2.
222.5 Arithmetic
- Many scripts perform arithmetic calculations
- Expressions in JavaScript must be written in
straight-line form
232.5 Arithmetic
242.5 Arithmetic
252.5 Arithmetic
Step 1.
y 2 5 5 3 5 7
(Leftmost multiplication)
2 5 is 10
Step 2.
y 10 5 3 5 7
(Leftmost multiplication)
10 5 is 50
Step 3.
y 50 3 5 7
(Multiplication before addition)
3 5 is 15
Step 4.
y 50 15 7
(Leftmost addition)
50 15 is 65
Step 5.
y 65 7
(Last addition)
65 7 is 72
y
72
(Last operationplace
into
)
Step 6.
y 72
Fig. 2.14 Order in which a second-degree
polynomial is evaluated.
262.6 Decision Making Equality and Relational
Operators
- Decision based on the truth or falsity of a
condition - Equality operators
- Relational operators
272.6 Decision Making Equality and Relational
Operators
?
28welcome6.html(1 of 3)
29welcome6.html(2 of 3)
30welcome6.html(3 of 3)
312.6 Decision Making Equality and Relational
Operators
32Lecture 2
- JavaScript I
- Control Statements A
332.7 Algorithms
- Actions to be executed
- Order in which the actions are to be executed
342.8 Pseudocode
- Artificial
- Informal
- Helps programmers develop algorithms
352.9 Control Structures
- Sequential execution
- Statements execute in the order they are written
- Transfer of control
- Next statement to execute may not be the next one
in sequence - Three control structures
- Sequence structure
- Selection structure
- if
- ifelse
- switch
- Repetition structure
- while
- dowhile
- for
- forin
362.9 Control Structures
- Flowchart
- Graphical representation of algorithm or portion
of algorithm - Flowlines
- Indicate the order the actions of the algorithm
execute - Rectangle symbol
- Indicate any type of action
- Oval symbol
- A complete algorithm
- Small circle symbol
- A portion of algorithm
- Diamond symbol
- Indicates a decision is to be made
372.9 Control Structures
Fig. 2.18 Flowcharting JavaScripts sequence
structure.
382.9 Control Structures
392.10 if Selection Statement
- Single-entry/single-exit structure
- Indicate action only when the condition evaluates
to true
402.10 if Selection Statement
412.11 ifelse Selection Statement
- Indicate different actions to be perform when
condition is true or false - Conditional operator (?)
- JavaScripts only ternary operator
- Three operands
- Forms a conditional expression
- Dangling-else problem
422.11 ifelse Selection Statement
432.12 while Repetition Statement
- Repetition structure (loop)
- Repeat action while some condition remains true
442.12 while Repetition Statement
452.13 Formulating Algorithms Case Study 1
(Counter-Controlled Repetition)
- Counter-controlled repetition
- Counter
- Control the number of times a set of statements
executes - Definite repetition
46average.html(1 of 3)
47average.html(2 of 3)
48average.html(3 of 3)
492.14 Formulating Algorithms with Top-Down,
Stepwise Refinement Case Study 2
(Sentinel-Controlled Repetition)
- Indefinite repetition
- Sentinel value
50average2.html(1 of 3)
51average2.html(2 of 3)
52average2.html(3 of 3)
53(No Transcript)
542.15 Formulating Algorithms with Top-Down,
Stepwise Refinement Case Study 3 (Nested Control
Structures)
- Consider problem
- Make observations
- Top-down, stepwise refinement
55analysis.html(1 of 2)
56analysis.html(2 of 2)
57(No Transcript)
58(No Transcript)
592.16 Assignment Operators
- Compound assignment operators
- Abbreviate assignment expressions
602.17 Assignment Operators
612.18 Increment and Decrement Operators
- Preincrement or predecrement operator
- Increment of decrement operator placed before a
variable - Postincrement or postdecrement operator
- Increment of decrement operator placed after a
variable
622.18 Increment and Decrement Operators
63increment.html(1 of 2)
64increment.html(2 of 2)
652.18 Increment and Decrement Operators
662.19 Note on Data Types
- Loosely typed
- Automatically converts between values of
different types
672.20 Web Resources
- www.javascriptmall.com
- developer.netscape.com/tech/javascript
- www.mozilla.org/js/language
68Lecture 2