OLPCities/JavaScript basic concepts

From OLPC
< OLPCities
Revision as of 16:28, 29 September 2006 by Adamascj (talk | contribs)
Jump to navigation Jump to search

Variables

JavaScript data can be objects or not. By example: if you declare:

x = "Peter";

we don't have an object. But you can declare:

x = new String("Peter");

and we will have an object of the Class String having methods etc.. The same for numbers and booleans. If we have "not typed" data, conversions are done automatically. By example:

x = "12";
y = 10;
z = '7';

And we can have:

total = x + y + z;

where "total" will be: 29.

A "no value" is assigned with the word: null.

Operators

 
+    Addiction
-    Subtraction
*    Multiplication
/    Division

The operator Modulus: % is curious. If :

x = 10 % 3;

The x value is: 1.

The % operator yields the remainder of its operands from an implied division; the left operand is the dividend and the right operand is the divisor.

The possible result of a boolean operation is : true or false. The operators are:

==    is equal to
!=    is not equal to
>     is greater than
>=    is greater than or equal
<     is less than
<=    is less than or equal

We can use parenthesis to define better a operation - or its precedence. If we write:

x = (4 >= 5);

The value of x is: false.

An usual error is to use the assignment operator: = instead the boolean operator: ==

It's usual the use of the logical operators in boolean operations:

&&    Logical AND
||    Logical OR

What is the x value?: x = (4>=5) && (2==2);

The answer is: false. BOTH need to be true to have a true.

And what is the y value?:

y = (4>=5) || (2==2);

The answer is: true. Only ONE needs to be true to have a true


Using the operator: // in a line of code means that nothing until the end of the line will be processed. Example:

x = 10 % 3; //We will have this.x=1 because 10=3+3+3+1

We can use /* ... */ for a multi-line comment. Example:

/* This program was created by XYZ Company
   Version : 1.43
   Contact for help: John Doe - ramal 678
*/

before the "real" lines of code of the program.


For concatenation of strings, for example:

x = 'ABC' + 'DEF';

The x value is: ABCDEF.

It's usual, if we have a "counter", to do an operation like that:

 
i = i + 1;

Is the same to use only:

 
i++;

There are a decrement operator. So, what is this?:

i--;

Sometimes we need to define a collection of elements: an array.

We can create an array and fill it with elements using, for example:

weapons = new Array('Pistol','Rifle','Lance');

We can "read" an element of an array using its index ( the first is ZERO!!!). So, if:

myWeaponNow =  weapons[1];

in the property myWeaponNow we have a: Rifle..

We can create an empty array and fill it later.For example, an array with 100 elements:

myClients = new Array(100);
myClients[0] = 'ADOBE';
myClients[7] = 'Microsoft';

Functions

A function is a group of statements (lines of code) created to do a defined action.

They are created to facilitate the logic of the program or if a same group of statements is used many times in the script.

A function can have parameters and/or can return a value.

Lets go to see lines of a program using a function we create and a "method" (a kind of function) of the Class String,(parseFloat) - that converts a string of algarisms to a number . Our function will have a parameter (the temperatute in Fahrenheit) and returns a value (the temperatute in Celsius).

//Our function:
 convertFC=function (fdegrees){ 
  cdegrees = (fdegrees - 32) * 5 / 9; 
 return  cdegrees; 
}

//This function will be used here:
 
num = new String("72"); 
x  = parseFloat(num);
y=  convertFC(x);
...

At "y" we will have the value converted.

TO BE CONTINUED