Vous êtes sur la page 1sur 4

March 25,

NOTES

Programming Errors:
Syntax errors are usually typographic errors
Run-time errors usually result from illegal values, some sort of
value that is illegal (do division by 0)
Logic errors are errors in the design or implementation of the
program
Relational Operators:
Used to compare values
The result is always a Boolean value
Compare if equal ==, not equal !=, less than >, greater than <,
or equal >=.
Does not confuse equality operatot (==) with assignment (=)
Logical Operators:
Combine Boolean values in couple ways
&&
!(not)
allows for more complex conditions
only get true value if 1st and 2nd operand are true
The Conditional Statement (if statement):
if (BOOLEAN_EXPRESSION) {
STATEMENTS_IF_TRUE
}
else {
STATEMENTS_IF_FALSE
}
{} are optional for single statements(shouldnt though)
if(79<score<90)
if ((score>79)&&(score<90))
Nested IF Statements:
if statements can be nested to any depth
indentation is important for readability but the interpreter
ignores it
eliminating braces can cause subtle errors
JAVA SCRIPT FUNCTIONS
Functions:
A named block of JavaScript statements

The function can be called by name to execute the block


Function can be treated as a single statement
Some functions are built into JavaScript
o Prompt(), alert(), document. write()
Parameters:
Just a list of variable names, values get assigned to variable
name
Parameters are the input to a function, the information a function
needs to execute
If your function has no parameters you still need parentheses
Local Variables:
Variables declared within a function exist only while the function
is executing
Variables declared outside of any function are global they can
be accessed from anywhere on the page undeclared variables
are always global even when used inside a function
Parameters are treated as a local variables that are automatically
initialized to the values passed into the function
Variable Scope:
Local variables in different functions can (and often do) have the
same name
Global and local variables can also have the same name
Local varuables have.
Return Values:
Some functions can output a value (return)
The return statement is used for functuion output
..
Calling a Function:
the arguments (function between parentheses) get evaluated
then the corresponding local variables get set up
the whole block of statements gets executed in order
if there is a return statement is evaluated
all local variables are reallocated
the value given by the return
Using Functions:
functions hide complex groups of statements behind a single
identifier
function calls can be nested within other functions

functions allow the reuse of code within the same page


o function definitions should be placed in the head section to
ensure that they are defined before being called
functions allow the reuse of code between many pages(let u
reuse code)
o all such function definitions should be placed in a separate
library file

Library Files:
A big list of function definitions
Put all function definitions is separate file with extension .js
Built in Functions and Objects:
JavaScript has built in functions
JavaScripts is object oriented language (all code is organized
into objects-variables and functions
Access them by dot notation
A lot of built in objects to use, and we can create our own objects
The Math Object:
20 mathematical functions
includes 20 useful mathematical functions including
exponentiation
o if a string is used, conversion to a number is attempted
o if conversion is impossible, function returns NaN
Useful Math Functions:
Math.sqrt(x)
Math.abs(x)
Math.floor(x)-takes a real number and always rounds it down to
the next lowest integer 1.999=1
Math.ceil(x)-always rounds up to the next highest integer
1.999=2
Math.round(x) .5=1 .4=0
Math.pow(x,y)-raises x to the y power
Maath.random()-generates a random number for us
Random Number:
Computers are deterministic, so they cant generate truly random
numbers. Only way to do it is to have some sort of device or
input into the computer that is random that comp can use.
There are algorithms that create pseudorandom numbers
The Math.random () functions utilizes one such algorithm

Math.random():
Generates a pseudo-random number xl o<=x<1
To generate a random integer between N and M:
Math.floor( (m-N+1)*Math.randm())+
The Document Object Model:
Many browser elements as well as aweb page elements are
automatically organized into an object hierarchy
The window object is the top level object
Web page elements are found in the document object
Accessing UI Elements:

UI elements can also be accessed by id attribute usinf the get


elemntBYID() function
Provides a more efficient way to unterat with user

Vous aimerez peut-être aussi