An Introduction to Programming using
JavaScript
- Program (typically):
- Input data (HTML -
Forms)
- Process this data, generate
results
- Output results (HTML -
Forms)
- Statements and Functions and
Programs (oh, my!)
- a program consists of a set of
functions
- a function consists of a set of
statements
- a statement is a list of symbols recognized
by the compiler language (for us, JavaScript)
- Events
- Objects - "things" on a page that also
have implicit functions.
- text
- image
- form object - used for our program I/O and
event trigger
- input object - input needed
data
- button object - call a function(s)
(i.e., process)
- textarea object - output
results
- Variables
- named places to hold data
- naming rules
- must start with an alphabetic
character
- remaining can be
alphanumeric
- can also use the character _
(underscore)
|
Good Examples
|
Bad Examples
|
|
x
|
42
|
|
index
|
#pounds
|
|
Max
|
99baloons
|
|
C3P0
|
Barbara Streisand
|
|
value1
|
^%$#*&@!
|
|
element_3_a
|
why?
|
- distinction between variable and
value
- Example: Squaring
a number
- Operators and Expressions -
Calculator
- arithmetic expressions
- evaluate to a numeric value
- Addition: op1 + op2
- Subtraction: op1 - op2
- Multiplication: op1 * op2
- Division: op1 / op2
- Remainder: op1 % op2
- Quotient:
uses Math.floor()
- Negation: -op
- Increment: op++ same as op = op +
1
- Decrement: op-- same as op = op -
1
- - "Better"
Calculator
- logical or boolean expressions
- evaluates to a boolean value (return
true || false)
- op1 < op2
- op1 <= op2
- op1 > op2
- op1 >= op2
- op1 == op2
- op1 != op2
- op1 || op2
- op1 && op2
- !op
- Syntax - valid rules for the
language
- Comments
- way to place English prose into your
program source for documentation (i.e., for
people)
- ignored by the compiler
- JavaScript supports two schemes for
comments
- one line: use
//
- multi-line: us
/* and
*/
- each function should be described using
comments
- your name should be included in the
initial program comments, as well as a program
overview
- Declarations
- variables and functions should be
declared before they are used (i.e.,
called)
- type information is necessary
- variables: what data
is held (and thus returned from any expression or to any
variable)
- functions: what data
is generated (and thus returned from any
expression or to any variable)
- in this way variables and functions are
can be used in the same way reserve words ...
var x, temp=3; //
variable
function solve(); //
function
- Expressions
- string of symbols consisting of
- operands (nouns)
- variables
- numbers
- string literals
- operators (verbs)
- arithmetic (e.g., +, -, *,
/)
- logical (e.g., <, >=,
||!=)
- Functions
- set of statements to accomplish a task
or perform a computation
- can be viewed as a subprogram
(modular)
- can be viewed like a math function,
which maps input to output
- input via parameters, which are
variables listed in the parens which are given values
outside the function
- output via the
return
statement
- like a true math function, a specific
input should produce a specific output
deterministically
- samples
- Control Statements
- Iteration (i.e.,
Looping)
- Sentinel
while() loop
- executes zero or more
times
- do not need to know how many times to
repeat beforehand
- repeat as long as some condition is
true
- logical condition expressed in the
parens of the
while() statement
- Power with
while() example
- Counting
for() loop
- executes zero or more
times
- (typically) need to know how many
times to repeat beforehand
- syntax is not intuitive
for(start; end-expr; iterate){
}
- start:
int index =
0
- end-expr:
index <
max
- iterate:
index++ or
index = index+1
- Power with
for() example
- Selection
- Parameters
- Strings, Characters, and Concatenation
examples
- Recursion
Page maintained by John
Dougherty
Computer
Science, Haverford
College