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.

Sunday, January 30, 2011

Week 1

For the first week of the class, I'm supposed to read chapters 1-3 in Eloquent Javascript and write reflections on the reading and answer the following questions:
  1. What is a type, and why do you think types are useful in writing programs?
  2. Why do we lose precision when performing operations with decimal numbers in Javascript? Can you think of a few implications of why this would be a problem?
  3. Do you understand why the following operation produces the given result 115 * 4 - 4 + 88 / 2 = 500
  4. What is the use of a backslash character in a String?
  5. What does typeof 4.5 do, and why does typeof (typeof 4.5) return "string" ?
  6. The author writes When your browser loads a page, it creates a new environment and attaches these standard values to it. The variables created and modified by programs on that page survive until the browser goes to a new page.. Without searching on the net, can you think of some variables that might be created in the browsers environment when it loads a page with Javascript in it?
Finally, I'm supposed to do exercise 2.1-2.6 and exercise 3.1-3.2 in Eloquent JavaScript.

All of this follows (all programs can be found at  http://userpages.umbc.edu/~flinchba/JavascriptPactice.html):

Eloquent Javascript Chapter 1
This chapter just talks about what Javascript is (and isn't). Again, it's not Java.   JavaScript is a type of ECMAScript plus extra tools for dealing with Internet pages and browser windows. ECMAScript 4 is being developed but old ECMAScript stuff will still work but new features will be added. The new features won't work until browsers integrate them. I did the count program described in this chapter although it wasn't an assignment: http://userpages.umbc.edu/~flinchba/count.html.
.



Eloquent Javascript Chapter 2

Calculations with whole numbers that fit in 52 bits are accurate. Large numbers must be done in scientific notations. Decimals don't calculate correctly.

Why do we lose precision when performing operations with decimal numbers in Javascript? Can you think of a few implications of why this would be a problem?
Because all numbers in Javascript are 64 bit leaving only 52 bits to store the numbers. This causes large numbers and decimals not to calculate correctly.

Operators are + - * / %. % is modulo and divides providing just the remainder. Same order of operations applies as in math. 

Do you understand why the following operation produces the given result 115 * 4 - 4 + 88 / 2 = 500?
You get this result by following general math order of operation.

Data types are numbers and strings.

What is a type, and why do you think types are useful in writing programs?
Types are the kind of data the program is dealing with. It gives the program information on what to do with the data.

\ within text indicates a special character. \n is supposed to make a line break but I've never got it to work.

 What is the use of a backslash character in a String?
The backslash used within quotes indicates that a special character follows.

+ concatenates strings.

typeof tells the type of value.

What does typeof 4.5 do, and why does typeof (typeof 4.5) return "string" ?
Type of 4.5 returns the value "number" because 4.5 is a number. typeof (typeof 4.5) returns string because here enclosed in the parenthesis "typeof" is just text.

Another data type is Boolean.

3>2 is a Boolean that returns a true value.
3<2 produces a false value.
Strings can be compared the same way: "Aardvark" < "Zoroaster"
Other Booleans:
>= ('is greater than or equal to')
<= (is less than or equal to)
== ('is equal to')
!= ('is not equal to')
&& operator represents logical and
|| is the logical or
Not is written as an exclamation mark, ! (the ! can also be put in front of something to flip the value from true to false or false to true)


Exercise 2.1
(4 >= 6 || "grass" != "green") &&
   !(12 * 2 == 144 && true)
 
is 
 
(false || true) && !(false && true
 
is 
(true) && !(false) 
is 
true
Program that prints numbers from 1-10: http://userpages.umbc.edu/~flinchba/count.html
Exercise 2.2: 
http://userpages.umbc.edu/~flinchba/2tothe10th.html
 
Exercise 2.3:  
http://userpages.umbc.edu/~flinchba/pyramid.html 
This doesn't work correctly because I couldn't get the \n to work. 
 
Exercise 2.4: 
http://userpages.umbc.edu/~flinchba/For2tothe10th.html 
http://userpages.umbc.edu/~flinchba/Forpyramid.html 
This also doesn't work correctly because I couldn't get the \n to work.
 
Exercise 2.5:
http://userpages.umbc.edu/~flinchba/prompttest.html  
 
Exercise 2.6:
http://userpages.umbc.edu/~flinchba/prompttestuntilright.html  
 
Comments:
'/*' starts a comment that goes on until a '*/' is found. 
'//' starts another kind of comment, which goes on until the end of the line. 
 
 
 
Eloquent Javascript, Chapter 3, Functions
 
function add(a, b) {
  return a + b;
}

show(add(2, 2));
 
add is the name of the function. a and b are the names of the two arguments. return a + b; is the body of the function.

Exercise 3.1
The parts that make this a function are noted out so the program will run independently.
  
Exercise 3.2
function greaterThan(x) {
  return function(y) {
    return y > x;
  };
}

var greaterThanTen = greaterThan(10);
show(greaterThanTen(9)); 
  

Monday, January 17, 2011

Why Javascript and Javascript Basics 1

Some ability to web program would be quite useful. At first I thought to learn Java and did compile a few small and vary basic programs. The basic issue I'm seeing with Java is that I can't as readily utilize it because my sites would require a Java server which I won't always necessarily have available to me. JavaScript on the other hand doesn't require a special server and can run from any web page.

There is a free and quite excellent web course available on Javascript, Javascript 101 at P2PU School of Webcraft taught by Parag Shah. The course includes video lectures, programming assignments, blog posts about what has been learned, and quizzes.

This is my first assignment on a video lecture given by Douglas Crockford of Yahoo that covers what Javascript is, it's history, key ideas, and values. The remainder of this post will be a summary of Douglas Crockford's content from the lecture:

Javascript was developed in 1992. It's not Java but a separate and distinct programming language. It's small but very effective and sophisticated. There are 5 things that make Javascript distinct as programming language. First, it's programs are delivered as text in a webpage through a browser, which is the quality that makes me believe it's the ideal web programming language for me to learn. Other qualities are loose typing, objects as general containers, prototypal inheritance, Lambda, and linkage through global variables.

Values in Javascript are numbers, strings, Boolean, Ojbects, null, undefined, and Nan (undefined or an error). Numbers are are floating point so when doing arithmetic with decimals you must first multiple to remove the decimals. Number(value) converts a string to a number and putting the prefix + in front of a string does the same thing. Boolean value are true and false. False values include false, null, undefined, an empty string, 0, and Nan, which may be confusing.

In Javascript, any type can be used anywhere, that is any variable can have any type of value. Even a function can have a value. In Javascript, all keywords are lower case and it's case sensitive. Constructors begin with upper case. By convention, $ and underscore aren't used in Javascript. Javascript has a long list of reserved words.

There are two ways of putting comments in Javascript. Line comments begin with // and block comments are /* comment */. Operators are + - * / === < >, etc. The plus both does addition and concatenates.  The plus prefix also converts a string to a number, eg. + "42" returns 42. &, //, and ! are types of if then and if not statements that I have to go back over and will need to practice to fully understand. Hopefully the class will cover them in more detail later.