Freitag, 28. Januar 2011

JavaScript Week 1

Reflection

Questions for reflection: Answers

1. A type defines what a value is and for what purposes it can be used.

2. The reason for this lies within how a number is presented in JavaScript. A number always has 64 bits. Now a floating point number has decimals and thus, it needs a dot somewhere between the digits. In numbers, 11 bits are used to position the decimal dot within the number. The reason that floating point numbers are not precise is because their fractional part can not be presented by finite amount of decimal digits. This is the reason that these numbers should not be treated as precise numbers, but only approximations.

Some situations I can think of where this could cause problems are programs that deal with finances or exact scientific calculations.

3. The rules of the binary operators "+, -, * and /" follow the ones in mathematics. "* and /" have a higher precendence than "+ and -". If two or more operators of the same precendence occur without any parentheses, then they are read from left to right. Example: 115 * 4 - 4 + 88 / 2 = 460 - 4 + 88 / 2 = 460 - 4 + 44 = 456 + 44 = 500.

4. The backslash signalizes that the first character that follows immediately behind it has a special meaning. For example, for the program to print text on a newline, one can't simply hit enter and go on writing. For that, '\n' would be suitable. These special characters are also known as 'escape sequences'.

5. It returns a string, telling what type 4.5 is. In this case, it is a number.

6. Maybe some of these are browser-information, operating system information, window height, window width, location of the user, cookies, history of browsing.


Exercise 2.1 

Yes, it results in True. Here is the evaluation:

(4 >= 6 || "grass" != "green") && !(12 * 2 == 144 && true)
(false || "grass" != "green") && !(12 * 2 == 144 && true)
(false || true) && !(24 == 144 && true)
(false || true) && !(false && true)
(false || true) && !(false)
(false || true) && true
true && true
true


Exercise 2.2

var number = 2;
var counter = 1;

while( counter < 10 )
{
 number = number * 2;
 counter = counter + 1;
}

show( number );


Exercise 2.3

var counter = 0;
var line = "";

while( counter < 10 ) // Iterate 10 times through the loop.
{
  line = line + "#";
  show( line, "\n" );
  counter  = counter + 1;
}


Exercise 2.4

var number = 2;

for( var counter = 1; counter < 10; counter = counter + 1 )
 number = number * 2;

show( number ); 


var line = "";

for( var counter = 0; counter < 10; counter = counter + 1 )
{
  line  = line + "#";
  print( line );
} 

Exercise 2.5

var answer = 0;

answer = prompt("What is the sum of 2+2 ?", "");

if( answer == 4 )
  alert("Very good, that's correct!");
else if( answer == 3 || answer == 5 )
  alert("Almost!");
else
  alert("Learn math, please.");  


Exercise 2.6

var answer = 0;

while( true )
{
  answer = prompt("What is the sum of 2+2?", "Type it in here.");
  
  if( answer == 4 )
  {
    alert("Correct!");
    break;
 
  else if( answer == 3 || answer == 5 )
  {
    alert("Almost!");
  }
  else
  {
    alert("Learn maths, please...");
  }
} 


Exercise 3.1

function absolute( number )
{
  if( number < 0 )
  {
    number = -( number );
  }
  return number;
}

show( absolute( -123 )) ;


Exercise 3.2 

function greaterThan( number )
{
  return function( number2 )
  {
    return number2 > number;
  };
}
var greaterThanTwenty = greaterThan( 20 );
show( greaterThanTwenty( 30 ) ); 

Welcome to this blog.

Let's start!