Tuesday, March 29, 2011

Lexical Scoping, nice!

First, writing about recursiveness, there is nothing more recursive than * power (base, exp -1), so it goes and goes until it reaches 0 and multiplied by 1. Too bad for the speed disadvantage of calling functions over and over...

Now, what can I say about Lexical scoping, seems great but its a little crazy to grasp at once, so here is my try at that using the makeAddFunction example.

makeAddFunction is created with one argument amount, and inside is just a function add with another argument number.
add returns the value of numer+amount and then makeAddFunction returns the result of the add function.
Here comes the important bit, the creation of 2 variables that will act as variations of the makeAddFunction, so we have the addTwo and addFive variables, both take the type makeAddFunction, one with one it's argument as 2 and the other as 5 (hence their names), this arguments are the values of  amount.
At the end they call this variables, with show, each with an extra argument, that will be passed to the add function inside makeAddFunction.

So we get this :   "show (addtwo(1) + addFive(1))"

that at the end is
"show (makeAddFuntion(2) add(1) + makeAddFunction(5) add(1))"

and this would be how it works, the value of addTwo/Five goes to number, because the makeAddFunctions had their arguments already given, in amount, while the variables addTwo/Five where created.

This way the programmer doesn't need to say "hey by the way, this little number at the end of addTwo, is the variable number that is inside the function, ok Js? good..." having the ability of getting a variation of functionality with less lines of code.

next the ex...

Tuesday, March 22, 2011


So the 3rd chapter  is about functions, I describe them as small programs inside a program, you "summon it" by its name and give them the information they need in order to finish their job...

The simplest example would be the addition so instead let me tell you the subtraction the function will be named "sub" and in order for sub to subtract it needs the 2 values to subtract, so you tell sub that you need to take 3 from 9, and then sub will tell you it is 6.

Functions are what libraries are made of, a bunch of functions for specific works, like OpenGL for polygon graphics, OpenAL for sound, most languages have a Math library for heavy mathematical calculations, also another for network connections and so on.

The idea of Functions is not to copy-paste so much code, but just to call it every time you need to make that job, making the program jump to look for that functionality.

The Ex. 3 from the book is done like this:

function absolute(value) {
  var result = 1;
  if (value < 0) result = value * -1;
  else result = value;
  return result;

Wednesday, March 16, 2011

Ex 2.6 while it lasts...

What I learned on this Ex, a var MUST be initialized, else the script won't run.
{} are very important when there are more than one statement, if sometimes is not too clear on it..
And, the condition variable must exist before the condition, or you will get a no run.
still not too hard, just need to pay attention to the keyboard.

var ans ="";
while (ans != "4") {
ans = prompt("2 + 2 = ?", "4");
if (ans == "4") {
breack; }
else alert("NO!, again...");}

continuer reading after this ex, and you can see that var=4 is the same as var="4", for comparisons.

Monday, March 14, 2011

Ex 2.5 if you do what I say...

This took a few minutes because I forgot to erase the } from the previous code... pay attention...

var ans = prompt("Hi, we need to test if the 4 key is working. Could you answer how much is 2 + 2 = ?", "just press the 4 key");
if (ans == "4"
  alert("The #4 keys are working perfectly, Thank you!");
else if (ans == "3" || ans == "5"
  alert("The key next to it...");
       alert("come on idiot, JUST PRESS THE 4 KEY!...");

2.4 rince and repeat, with for

this is the 2.2 

var value = 2;
for (var power = 0; power <10; power++) {
   value = 2 * value; show (value); }

this is the 2.3

var chain = "#";
for (var cont = 0; cont <10; cont++) {
  print (chain); chain = chain +"#"; }

Ex 2 "triangles" with strings

based on 2.2, 

var cont = 1; chain = "#"; while (cont <= 10) {
  print (chain); chain = chain +"#" ; cont = cont + 1; }

I print first to get the original "#" and then start to add more "#" on every loop and we get a rectalngle triangle.

Ex 2.2 2 to the 10th power

Using while it goes like this...

var power = 1; value = 2; while (power <= 10) {
  value = 2 * value
  show (value);  
  power = power 1; }

2 vars, one for control and the other one to show the result.

Wednesday, March 9, 2011

ex 2.1 true or false!...

(4 >= 6 || "grass" != "green") && !(12 * 2 == 144 && true)
As we can see here, we have 2 main comparisons, being compared, an or and nand situation.
The solution for the or is between false or true, and that is true.
The inside and is not false and true which is true or not false.
And finally the main and between true and true, with a result of true
:) is a nice ex for the brain, could there be any other like this with trues and false online...
hardest logic puzzle ever
this is not one but its an interesting read!.

Friday, March 4, 2011

week 2, the others are comming...

Better late than never!... . .  .

After reading chapters 1, 2 and 3 from the book, here are the important bits, I will clear all the theory first and then post for every exercise.

Types are the definition that variables need, in order to know what they represent. The main ones on the are: Numbers, strings, Boolean, objects, functions, and undefined values. This where created so the programs would know when they are using numbers of letters or any other thing on execution, this way it treats 2+2 as an addition or as the string 2+2 (as characters).

For calculations we would lose precision when dealing with too many decimals, it would be a problem if we need that level of precision in calculations, no solutions was presented on this chapters about it, since, most of the time, general calculations fit the limits of JS.

And as Alex pointed out, "as I was taught in Calculus: Multiply First, resolve everything above the division, then divide… its simple arithmetic"

What is the use of a backslash character in a String?, well is to insert special characters, the most common (in all programing languages) is /n as the end of line.

Typeof returs, as the resutl of its operation, a string indicating the type of the value to evaluate, typeof 4.5 tells you that 4.5 is a number, typeof (typeof 4.5) indicates it as a string because the result of typeof 4.5 is the string "number" so the typeof "number" is a string...

and now the exercises..