CpSc 217 - Structured and Dynamic Web Programming
The notes found on these pages all reference the textbook given above.
Unless otherwise stated, the citation of the information on these web pages
is that text.
This chapter is a partial
Review from 130
Numbers
- Integers: 0, -5, 678
- Floating Point: 3.14, .3333, 6.02e23, 1.473E-32
- Arithmetic: +, -, *, /. %
- Math Object (see page 33 of text for complete list
- Math.round(.6)
- Math.random() //0 <= x < 1.0
- Math.pow(2,5)
- Math.floor(.6)
- Math.ceil(.6)
- Predefined Global Variables you should know
- Infinity
- Number.POSITIVE_INFINITY
- 1/0
- Number.MAX_VALUE + 1
- Number.NEGATIVE_INFINITY
- -1/0
- -Number.MAX_VALUE - 1
- NaN
- Number.NaN
- 0/0
- Floating point rounding errors
Sample
var x = .3 - .2; // 30 cents minus 20 cents
var y = .2 - .1; // 20 cents minus 10 cents
x == y // false !! not the same
x == .1 // false !! not the same
Date Object
-
var later = new Date(2013, 0, 1, 17, 10, 30); //Jan 1, 2013 5:10:30pm
Sample
- Date Methods: later.getFullYear()
- later.getMonth() // 0
- later.getDate() // 1 first of month
- later.getDay() // 2; 0 for sunday, 1 for Monday,...
- later.getHours() // 17 which is 5pm
Strings
- null
- backslash is used to escape special characters
- single quotes or double quotes
- \n, \t
- String methods (page 38-39)
- s.length
- s.charAt(0)
- s.substring(1,4) // 2nd, 3rd, and 4th characters
- s.slice(1,4) // 2nd, 3rd, and 4th characters
- s.slice(-3) // last 3 charcters
- s.toUpperCase() // returns upper case - does not change string
- s.replace("e","E") // replaces all lowercase e with uppercase - does not change string
- s.indexOf("x") //index of where x is in the string
- s.split(",")
- Try it
See page 254 for regular expression special characters
- Sample
- Pattern Matching - text between slashes interpreted as a regular
expresion - /expression/
- ^ - starts with
- [0-9] range
- a* any number of a's
- \bword\b - match as a word
- \s - white space
- \S - not white space
- \d - digit
- \D - not a digit
- \w - any ASCII word character (letter, digit, number)
- a+ one or more a's
Booleans
Global Objects
- global properties: undefined, Infinity, NaN
- global functions: parseInt(), eval(), isNaN()
- constructor functions: Date(), RegExp(), String(),
Object(), Array()
- global objects: Math, JSON
Wrapper Objects
Immuatable and Mutable Objects
-
Immutable Object
var s = "hello"
s.toUpperCase() //returns uppercase, but doesn't change s
-
Mutable Object
var o = {x:1}; // object with single property
o.x = 2; //change or mutate o by changing x
o.y = 3; //mutate o by adding a property
- Arrays are also mutable
- Comparing objects:
- arrays - array a is not equal to array b unless set equal (a=b). They
are reference types. To compare two arrays, use a loop
- Objects are also reference types. Have to write code to compare
each property
- Try both
Type Conversions
-
Will attempt to convert on the fly. See table 3-2 on page 46
-
Explicit Conversions:
Number("3") // returns 3
String(false) // returns "false"
Boolean([]) // returns true
Object(3) // returns new Number(3)
({x:1, y:2}).toString() // returns "[object Object]"
[1,2,3].toString() // returns "1,2,3"
Variable Scope