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)); 
  

No comments:

Post a Comment