Review from 130

Download Report

Transcript Review from 130

Javascript Essentials
How do I write it??
Start Homesite
Between the start and end BODY tags
type:
<script language =“Javascript”
<!-- hide script from old browsers
 alert('Welcome to my Web Site!');
 //-->
</script>
Save and Browse
*
Javascript Basics
•
• Data “Types”
• Declarations
• Expressions
Data Storage
Javascript Data Types
String
- default type
Integral -ordinal numbers
-Use parseInt()
Floating -real numbers
-Use parseFloat()
Characters and Integers
01000010

char
(1 Byte)
256 possibilities, ASCII characters
enclosed in single quotes: ‘B’

01000001 01000001
int (2 or 4 Bytes)
no decimal points
no commas
no special signs ($, ¥, ‰)
***
Floating Point Number Data
Types
 Floating point numbers:
signed or unsigned with decimal point
 Types: differ in amount of storage space;
varies with machine

 float (single precision)
 double
 long double
***
Variables

/
/

a place to store information
contents of a location in memory
/ has an address in RAM
/ uses a certain number of bytes
Name
num1
Type
int
Location
1000
1000
-9999
*
Identifiers

Valid examples:

x
num1
num2
c237
index
tax_rate
name
row
Not valid:
1num
bye[a] tax-rate
OK:
newPos
newpos (beware of this!)
tax rate
***
Identifiers
Rules for Naming Identifiers


Can contain digits, letters, underscores
NO SPACES

Begin identifier name with a letter

Limit Identifiers to 20 characters
***
Variable Definition
Associates an identifier with its use
 Can be done with an assignment
 or must be done with documentation
Identifiers should have meaningful names.
- Reduces confusion
- Very few one letter names are acceptable:
i, j, k, l, x, y, z (others?)
- Two letter names? PI
****
Variable Definition
 var identifier_list;
 var i, k;
 var taxRate = 0.087;
 var len = 10, wid =20;
<script language =“Javascript”>
var name = “Joe”;
alert('Welcome to my Web Site!‘+ name);
</script>
****
Literals & Constants
Literals: 'Y', 23.4, -1, "Hello"
typed directly into the program as needed
ex. y = 23.4
pi = 3.1416
Literal values are stored in anonymous
memory addresses by the compiler
***
Operators

An operator is a symbol that causes
the compiler to take an action.
Categories:
mathematical
assignment
boolean/logical
etc.
*
Binary Arithmetic Operators

addition

subtraction

multiplication
*

division
/

modulus
Unary Minus operator:
+

%
x = -3;
y = -x;
*
Arithmetic Operators
Increment and Decrement
Arithmetic Expressions
Syntax
operand operator operand
Example
7
34
92
345
86
+
15
—
*
/
%
189
31
6.02
3
*
Integer Division & Modulus

The modulus operator yields the
remainder of integer division.
4
3 12
12
0
12/3
14/3
12%3
14%3
4
3 14
12
2
**
Modulus Examples

The modulus operator yields the
remainder of integer division.

18
17
24
4
12
%
%
%
%
%
4 is 2
3 is 2
6 is 0
18 is 4
2.5 error
13
35
24
0
6.0
%
%
%
%
%
4 is 1
47 is 35
4 is 0
7 is 0
6 error
Requires floating point division! Not the same.
****
Operator Precedence
Order of Operations
() * /
+ -
P M D A S from left to right
8 + 3 * 4 is ?

Show associativity to clarify.
(8 + 3) * 4
is 44
8 + ( 3 * 4 ) is 20
**
Order of Operations

Expression
15
Value
10 / 2 * 3
10 % 3 - 4 / 2
-1
5.0 * 2.0 / 4.0 * 2.0
5.0
5.0 * 2.0 / (4.0 * 2.0)
1.25
5.0 + 2.0 / (4.0 * 2.0)
5.25
*****
Evaluation Trees
10 / 2 * 3
10 % 3 - 4 / 2
i
i
i
i
i
5.0 * 2.0 / 4.0 * 2.0
5 * 2 / (4.0 * 2.0)
i
r
r
r
r
r
**
Evaluation Trees
5.0 + 2.0 / (4.0 * 2.0)
r
r
r
5.0 * 2.0 / 4.0 * 2.0
r
(5 + 2) / (4.0 * 2.0)
r
i
r
r
r
Beware!

The asterisk is required to indicate
multiplication.

valid
5*(8+3)

(x-y)*(x+y)
invalid
5(8+3)
(x-y)(x+y)
Assignment Operator
The assignment operator (=)causes the
operand on the left to take on the value
to the right side of the statement.
This operator assigns from right to left.
valid
invalid
name = “Sue” “sue” = name
*
Assignment Statement
Syntax: variable = expression;

Examples:
quiz1score = 14;
lname = “Newman”;
fname = “Alfred E.”;
wname = fname + “ “ + lname;
Do NOT use the word equals here.
Use is assigned to or gets.
*
Storage Locations
1.
2.
3.

deposit = 234;
balance = 1000;
balance = balance + deposit;
4a. Look up balance value
4b. Look up deposit value
4c. Add values
4d. Store result in balance
*
Storage Locations
deposit
balance
234
100
After assignment: balance = balance + deposit;
234
1234
6 bytes of storage
q
w
e
r
t
y\
Assignment Operators
String Concatenation
Relational and Boolean
Operators
Conditional Statement (if)