Tuesday 1 February 2011

Week 1 Exercise 3.2 Function greaterThan that returns a function

Write a function greaterThan, which takes one argument, a number, and returns a function that represents a test. When this returned function is called with a single number as argument, it returns a boolean: true if the given number is greater than the number that was used to create the test function, and false otherwise.

On my first attempt I was not what the question was trying to get me to do.

function greaterThan (a){
  function test (y){
    if (a < y) {
      return true;
    }else {
      return false;}
  }
    show (test (50));
  }
  show (greaterThan(3));
 
After reading the solution, it was a bit more clear:

function greaterThan (a){
  return function test (y){
    return y > a;
};   
  }

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

The last few parts of chapter two of the book that concerns anonymous function and recursion was more complicated. It requires multiple read through.

Week 1 Exercise 3.1 Function to return the absolute value of a number

Ex 3.1

My attempt to create a function that returns an absolute value of a number. I created an if else statement. If the argument is greater than 0, you return the value. Otherwise, multiply the number with -1 and return that value.

function absolute (a) {
  if (a > 0) {
    return a;
  }
  else {
    a = -1*a;
    return a;
  }
}

Monday 31 January 2011

Week 1 Exercise 2.6 While and Optional Break

This was my answer. I did not take into account the else if statements.

var answer = prompt ("What is the value of 2+2?");
while (answer != "4"){
  var answer = prompt ("No, try again. What is the value of 2 + 2?");
  if (answer == "4")
    break;
      }

The solution provided by the book was more elegant. It used the while statement that if true, will continue to run until the break.

var answer;
while (true) {
  answer = prompt("You! What is the value of 2 + 2?", "");
  if (answer == "4") {
    alert("You must be a genius or something.");
    break;
  }
  else if (answer == "3" || answer == "5") {
    alert("Almost!");
  }
  else {
    alert("You're an embarrassment.");
  }
}

Week 1 Exercise 2.5 If Else Statement

My first attempt was wrong because when I use Logical Or, I for got to put the whole expression

var answer = prompt("What is the value of 2+2?");
if (answer == "4")
  alert ("well done, you are great with maths!");
else if (answer == "3" || "5")
  alert ("Almost!");
else
  alert ("You are too stupid to live");


The correct answer is this:

var answer = prompt("What is the value of 2+2?");
if (answer == "4")
  alert ("well done, you are great with maths!");
else if (answer == "3" || answer == "5")
  alert ("Almost!");
else
  alert ("You are too stupid to live");

Week 1 Exercise 2.4 Rewriting using For

2.2 Rewrite for
Initially I failed because I did not understand how to put the statements regarding the currentNum variable into the for statement. When I looked at the solution, I understood that the for loop is only used for one variable, namely count. You put the rest of the statements in another block.

currentNum = 2;
for (var count = 1; count <10; count +=1){
  currentNum=currentNum * 2;
}
print(currentNum);


2.3 Rewrite using for

currentString = "x";
for (var count = 1; count <=10; count +=1){
  print(currentString);
  currentString = currentString + "x";
}

Week 1 Exercise 2.3 Drawing Triangles

I modified exercise 2.2 to print out 10 lines of #, adding one more # to each line.

currentString = "x";
var count = 1;
while (count <= 10){
  print(currentString);
  currentString = currentString + "x";
  count +=1;
}

Week 1 Exercise 2.2 Two to the Power Ten

I created a variable count. As part of the function, 1 is added to the value of count after each loop. Initially I in the while statement, I used while (count <=10). That was incorrect because when count is 1, 2 to the power 1 is still 2, so you are not actually multiplying by 2. The loop will only have to run 9 times. The way to correct this is either start the count as 2 or use (count < 10) instead of (count <=10).

currentNum = 2;
var count = 1;
while (count <10) {
  currentNum= currentNum*2;
  count +=1;
}
print ( currentNum);

Week 1 Exercise 2.1 Parantheses Necessary?

The two initial statement below are both true. The few parentheses could be omitted from the first statement.

((4 >=6) || (“grass” != “green)) && !(((12*2) == 144) && true)

((false) || (true)) &&  !(((24 == 144 )&& true)

((false) || (true)) &&  !((false)&& true)

((false) || (true)) &&  !((false)&& true)

(true)&&!(false)
(true) && (true) returns true because both values are true



(4 >= 6  || “grass” != “green”) &&  ! (12 * 2 == 144 && true)

(false || true) && !(false && true)

true && !(false)

true && true

Week 1 Questions for Reflection

1. What is a type, and why do you think types are useful in writing programs? 

Type separates data values and determine what they can do and what the programmer can do to it. There are six basic types in Javascript: Numbers, strings, booleans, objects, functions and undefined values.

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?

Some decimal numbers are only an approximation when expressed with Javascript. Javascript’s Number Type only has 64 bit floating point and therefore can only approximate decimal numbers that have a infinite decimal value, such as pi or the value of 10 divided by 3. (10.3333333)

A problem that could arise is with money. The imprecision with decimals can lead to accounting errors.

3. Do you understand why the following operation produces the given result 115 * 4 - 4 + 88 / 2 = 500

With the about operation, the multiplication and the division is performed first, so we get
460 - 4 + 44 = 500 

 4. What is the use of a backslash character in a String?

The back slash character in a string tells the string to treat what immediately following the backslash character to be a special character. 

For example:
\n is the same as new line
\t means tab
\\ means just the character backslash \

 
What does typeof 4.5 do, and why does typeof (typeof 4.5) return "string" ?

The typeof operator returns the Type of the value given to it.

e.g. typeof 4.5 returns the value of  Number

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?

Some of the variables could be standard mathematical constants such as pi or internal set variables used by the program, such as the boolean values of certain internal variables and variables used in the definition of various functions.

Wednesday 19 January 2011

Pre-Entrance Task 2: HTML page with alert.

I was required to create an html page that displays the result of 2 + 2 via an alert when the page loads. My code was follows:

var x = 2;
var y = x + x;

alert("The result of 2 + 2 is " + y);

I simply defined two variables. x and y. The alert was a simple concatenation of the string and the value of y.

Click here to see the html page

Pre-Entrance Task 1: Javascript Basics Video 1 - Learning Journal

I have learned the brief history of Javascript and how despite its name suggesting otherwise, it is not related to Java and it is a real programming language. I also learned that there were certain inherent mistakes that had carried over since its conception. These mistakes in its design, can cause potential programming problems that the coder has to be aware of.  I learned of a few today.

One such nuisance was the addition operator. It acts as both addition for numbers and concatenation of strings similar to other languages. But if one of the operand is not a number, it will convert both into strings and concatenate them.

The second was that it only has one number type and it is the 64bit floating point, which may cause a problem dealing with money because floating points can only give you an approximation of the figure. To counter that one should always convert any currency into its smallest unit and do the mathematical calculations as integers and then convert the number back into decimals.

The third mistake is Javascript’s large list of reserved words, even though only a small proportion of the words on the list are used in the language. You are also not allowed to use any reserved words in  creating names using the dot notation.

The forth point to be aware of  is the use of == and != operators. The equal and not equal operators can do type coercion. So it was recommended that you use === and !== if you want exact equality and inequality.

I have also learned a strange value called NaN which, stands for Not a Number.  If anywhere in your programming produces a result of NaN, you final value will also be NaN (toxic). It does not equal to anything, not even NaN, but it is a Number.


The key ideas of the Javascript language listed were:
Load and go delivery
Loose typing
Objects as general containers
Prototypal Inheritance
Lambda
Linkage Through global variables.

I am looking forward to learning more about Javascript when the course starts.