Sunday, February 6, 2011

Week 2

Properties—values associated with other values
Length--a property of a string that refers to a number, the amount of characters in that string.
Eg. document.write(text["length"]);
 
To give a variable properties:
var cat = {colour: "grey", name: "Spot", size: 46};
 
To give a property a new value:
cat.size = 47;
 
Arrays—Collection of similar objects
 
New arrays can be created using brackets ([ and ]):
var mailArchive = ["mail one", "mail two", "mail three"];
 
Methods—Properties that contain functions such as the properties toLowerCase 
and toUpperCase.
 
  • Push is a property that adds values to arrays.
  • Pop is a value that takes off and returns the last value.
  • Join builds a single big string from the array.
  • Split splits a string into an array using the value given to it to chose where to split.
  • charAt can be used to get a specific character from a string--x.charAt(0) gives the first character, 1 is the second one, and so on.
  • Slice copies out a piece of the string, starting from the character at the position given by the first argument, and ending before (not including) the character at the position given by the second one. This allows the check to be written in a shorter way.


Date object. Examples:
var when = new Date(1980, 1, 1);
show(new Date());
show(new Date(1980, 1, 1));
show(new Date(2007, 2, 30, 8, 20, 30));
Use the Get method to find parts of a date. Examples:
var today = new Date();
print("Year: ", today.getFullYear(), ", month: ",
      today.getMonth(), ", day: ", today.getDate());
print("Hour: ", today.getHours(), ", minutes: ",
      today.getMinutes(), ", seconds: ", today.getSeconds());
print("Day of week: ", today.getDay());
You can compare dates with <, >, <=, and >= 
 
 
Math functions:
cos, sin, tan, acos, asin, atan
pow--power
round, floor, and
ceil will round numbers to the closest whole number, the whole
number below it, and the whole number above it respectively.  
 
This is a good Javascript reference: 
http://www.webreference.com/javascript/reference/core_ref/contents.html 
 
 
Error Handling
 
If a function can only handle certain types of values, then it will fail if not given a value of that type, so there needs to be algorithms for handling unexpected values.

Eloquent Javascript Exercise 3.2 requires creating a function that finds out if one number is greater than another number. I've added on to this by having the program take two user input numbers. It works for numbers 1-9, but above that it's iffy. My program thinks that 4>22!! I obviously need to work on understanding those numbers better: http://userpages.umbc.edu/~flinchba/GreaterThanFunction1.html

Eloquent Javascript Exercise 4.1 requires creating a function that finds the absolute value of a number: Absolute Value Program.

Eloquent Javascript Exercise 4.2 requires creating a function to show the range between two numbers:Range.

Eloquent Javascript Exercise 4.3 requires demonstrating that the split and join properties aren't exact inverses. The program joins an array, then joins it and splits it, getting a different result than the original array here: Split/Join.

Again, all of my programs are linked from this page: file:///C:/Javascript/JavascriptPactice.html.