Monday, June 3, 2013

learn a word a day 6 : perambulate

So a new chapter in learn a word a day

today's new word is perambulate:

: walk or travel through or around esp for pleasure and in a leisurely walk
: walk from place to place, walk about

ex : I like to perambulate regularly around the streets.
I perambulate to clear my stupid head.

My determination levels for this project are dropping now, as you can see i was not able to write the previous words. I also notice that each of my posts varies a lot since, they are changing their pov , sometimes i or you. i hope i don't sound facetious to you. But if you are a fastidious reader you can clearly see the mistaken use of pronouns in my blog. But you also will notice the motif of writing this blog, so i hope you can forgive me. I want self as well as your impetus to continue and prolong this project. I hope you understood what i meant here.

TARGETS AND GOALS

I kept three targets till sunday, i was able to complete only one of them. That is 'learn a word a day'

I completed another target by today, so i am some what glad , i completed two of my targets. And i failed in one, that is algorithm a day.

Now i am realizing the action method stated in 'Making Ideas Happen' , i need to act first to see how the output is coming and i realized that two of the projects were easily made rather than pondering on selecting which one to complete, i tried to do all and finished two of them.

So from now on, no pondering only executing. that is the golden word or keyword whatever it can be.

So new targets, since i already completed javascript, now since i also realized how much time i am wasting, i will have to try to minimize time waste and increase my productivity this time to finish my targets.


This week targets are :

JQUERY
C, C++ PROGRAMMING : 25 PAGES A DAY
LEARN A WORD A DAY
TOPCODER PROGRAMMING ONE PROBLEM A DAY

GET A JOB -------TO VICTORY

Yes!, i finally completed my first training course. Yes, i have done one thing completely without interruption for the first time in a long time. Looks like i am getting my mojo back. But i don't feel the elation and triumph, i see much bigger obstacles ahead in my path to execution.

For the first time in my life, i am setting myself a goal in my life, i want to get a job, a good software development job. That is my goal and i will strive for it with full on determination.

This is my first win in a battle, but the war is still being waged. Only way to end the war is victory.

To victory..........

javascirpt lesson 10 -- continued

In javascript, by default all properties of an object are public.

Just as functions have local variables, which can be accessed only from inside a function. we can also have private variables in a class which we won't share publicly.

if create a variable without using 'this' keyword in a constructor function, then it is a private variable.

ex :

function Person(name){
this.name = name;
var balance = 234;
}

var sur = new Person("bob");
console.log(sur.balance); // not accessible, gives undefined output

*** Even though we cannot access private variables directly, we can access them by using a public method. this method is very similar to access a private variable.
ex:


function Person(first,last,age) {
   this.firstname = first;
   this.lastname = last;
   this.age = age;
   var bankBalance = 7500;

   this.getBalance = function() {
      // your code should return the bankBalance
      return bankBalance;
   };
}

var john = new Person('John','Smith',30);
console.log(john.bankBalance);

// create a new variable myBalance that calls getBalance()
var myBalance = john.getBalance();
console.log(myBalance);

A method can return another method inside a method.
ex:

this.askTeler = function(){
       return returnBalance();
   };

********** types of variables we are concerned with are 'number' 'string' and 'object'

javascript lesson 9 -- object oriented programming

when i was making a constructor in the previous lessons, i am infact making a new class.
A class can be thought of as a type, just like how 'number' and 'string' are types

when i made two objects bob and susan in previous lessons, they infact belong to class Person.

prototype keeps track of what a class can have property or method. the prototypes in javascript are automatically defined with a constructor.

*** if we want to add a method to class prototype, such that all the objects of this class can use the method. then we have to do this to extend the prototype
syntax:
classname.prototype.newmethod = function(){
            statements;
};

function Dog(breed){
this.breeds = breed;
}
Dog.prototype.bark = function(){
  console.log("woof");
};                                             // we added a bark method so other objects can use this method.

**Inheritance:
it allows one class to see and use the properties and methods of another class.

**DRY  -- Don't Repeat Yourself principle of programming.

** to inherit class1 properties to class2, we need to set the prototype of class2 to be class1.
ex:

// the original Animal class and sayName method
function Animal(name, numLegs) {
    this.name = name;
    this.numLegs = numLegs;
}
Animal.prototype.sayName = function() {
    console.log("Hi my name is "+this.name);
};

// define a Penguin class
function Penguin(name){
    this.name = name;
    this.numLegs = 2;
}

// set its prototype to be a new instance of Animal
Penguin.prototype = new Animal();

**the motif i noticed here is that to add any thing to a class like a method or inheritance of another class we use prototype.

* If javascript can't find something in the current class, it goes to the top of the prototype chain, to see if it from a class it inherited from.

This goes up the way upto the top Object.prototype . By default all classes directly  inherit from Object class, unless we change the class's prototype.

javascript lesson 8 very important

contructor can be thought of as a template.

For retrieving property values , we have two ways, dot notation and bracket notation

bracket notation has an advantage where we can use variable which store property names as well.

ex :
var somobj = { propname : "propval"};
var p = "propname";

somobj[p];             // this also means somobj["propname"]  , hence this is the advantage.


** sometimes we need to know the type of variable we are using, it can be 'number', 'string','function' or 'object'.

so to find this , we have a function to know this, 'typeof()'
ex :

var a = { b : "dfd"};
console.log(typeof(a));        // o/p is object

****** All objects have a useful function, which tells if an object has a certain property or not, that is 'hasOwnProperty'
ex:

return somobj.hasOwnProperty('name') ;    // returns true or false, if it has that property then true.


** for in loop can be used to print all the property names.
we can also use this to print the property values if we use bracket notation.

ex :
var somobj = { type : "indie" }

for ( var prop in somobj){
     console.log(prop);                    // print the name of property, i.e, o/p is type
     console.log(somobj[prop]);      // print the value of property


Sunday, June 2, 2013

javascript lesson 7

As we think of property in a object as a variable associated with an object.

methods in a object can be thought as functions associated with an object.

ex:
   obj.method = function(){

};

methods can be used to change property values.

if we want to use the method for other objects as well, for this we use 'this' keyword.

first we define a method

var setAge  = function(newage){
this.age = new(newage);
}

now we define our object

var obj = new Object();

obj.age = 22;
obj.setttingnewage = setAge;        //this acts as a placeholder, it will store whichever object called this
obj.settingnewage(23);             // method, so now whenever  we type obj.settingnewage(23), this.age
                                                   // in the method will refer to bob.age . here this holds obj, since obj called it.

***we can define objects inside objects in javascript.

I tried to create objects by 2nd method using 'new' keyword. here 'Object' is the constructor. it is already defined by javascript. Now i am learning that we can make custom constructors as well.

the Object function constructor creates a object with no properties and we have to manually add them.

so we create custom constructors instead.
ex:

function Person(name, age){
this.name = name;
this.age = age;
}

so now we have 'Person' constructor, we can create person objects much easily here

var bob = new Person("bob", 22);   // properties are already defined

we can also initialize properties inside a constructor not just define them.
ex:
function Person(name, age){
this.name = name;
this.species = "Homo Sapien";
}                // we initialized the property species here.

**** Not just properties, we can also define methods directly inside constructors.

**** I know object is just like string or integer just like any variables. like we have done a array of integer and strings, we can also make a array of objects

ex : var family = new Array();
       family[0] = new Person("sur",22);
       family[1] = new Person("bur",23);


arrays filled with objects will work just like arrays filled with strings or integers.

*** we can send objects as argument to a function, just like we send int or string as arguments

Math.PI -this func gives the value of pi

javascript lesson 6 :

there is another loop called 'for in' loop

this loop loops through the properties of a object

syntax:

for (var something in object){

do something
}


since the object is defined as key-value pair here
ex:
var obj = { a : 'd', b:'d'};   // a, b are keys and their values are d, d respectively

for ( var prop in obj){
console.log(prop);
}   //////////here the o/p is a, b  since the this loop is looping through the object keys


*** if we want to use the object value instead of the keys of the object. we can do

obj[prop];  // this is value of the key

***    !==   this is not equal to operator

I wasted a lot of time yesterday night, today is a new day. And my impetus for completing this
training is gone. I have to try to fight my distractions as well as the loss of my determination. I still need to continue. I hope writing this blog shows that i hadn't left  this training yet. So this is the first step towards regaining my lost impetus to complete this project and not sound facetious and to try to get back to the fastidious methods i employ while doing these kind of things.



*** once again to create an empty object example is below

var bob = {};

each piece of info we add in a object is known as property
ex:
var bob = {
   age = 22
};                            // here age is a property and 22 is the value of this property

to access a value of a property we use '.' dot notation
ex:   var a = bob.age;   //here we are accessing the value of age property

we can also access by this as well

var a = bob["age"];



Another way to create object is to use keyword 'new' . this method is known as creating an object using a constructor.
ex:

var bob = new Object();

learn a word a day 5

My will is low, as the end nears. what to do, how to motivate myself to push past the barrier which is visible yet unattainable. But i have to do it, come on you can do it.
the motif of my many failures is leaving things when things get tough. Now things got tough and are going to get tougher, i hope i can persevere. I should!

the previous words of the day are

facetious, fastidious, motif, impetus

today's word of the day is ' impetuous '

: acting or done quickly without thought or care, impulsive
: moving forcefully or rapidly

ex: i am impetuous person, otherwise i wouldn't have fucked my semester exams. 

Saturday, June 1, 2013

learn a word a day 4

I am wasting a lot of today as well. But today, it is not because of my distractions but of low determination levels. I need to regain momentum to complete this javascript training as soon as i can, mostly by tomorrow.

Come on!!!!!!! you can do

so for the learn a word a day

previous words are facetious, fastidious, motif

today's word is impetus
:momentum of moving body, a driving force
: an impelling force, impulse
: something that incites

ex:  my emotional impetus is always draining, i don't know how to rejuvenate it. And my fastidious principle is also not working. As you can see the motif of this blog, i hope you don't see the posts as facetious remarks.

javascript lesson 5 --very important

the objects in javascript are not similar to that of 'C++'

the objects can store all kinds of variables , including functions.

objects are like arrays, but in arrays the keys are numbers and values can be in anything
but in objects , keys can be anything as well.

the syntax for objects is

var object = {
key : value,
key : value,
key :value
};

ex:
var obj = {
 name : "felix",
age : 22
};

***actually , it is curious to see that there is no concept of 'class' in javascript. All the object are prototype-based inheritance schemes.
new objects are created by copying other objects(instead of being instantiated from a class template)

methods live directly in objects instead of classes

inheritance is done through delegation

Javascript has a nice concept tweak of constructors, you can copy existing objects with constructors as well create objects out of thin air

*** two important articles about inheritance are here
http://javascript.crockford.com/prototypal.html
http://javascript.crockford.com/inheritance.html

we can actually create objects here in two ways

the previous above method is first method with the use of {}
the second method is by the use of the constructors.

ex:

var myobject = new Object();      //this tells computer to make a new object type

myobject.name = "felix";  //we can use this method or method below which uses key
myobject["age"] = 22;

Javascript lesson 4

here i am learning about another javascript inbuilt library function

For generating random numbers, we use

var randomno = Math.random();     //this creates random betw 0 and 1

to get a random whole number, we do

Math.floor(Math.random()*5 + 1);  // this generates random whole number betw 1 and 5

** isNaN()  : it checks to see if it is not a number
ex: isNaN("berry")            here the output is 'true'

There is also a 'switch' in javascript to avoid many if/else's
The syntax is exactly like that of 'C' language
syntax

switch(expr){

case 'option1':
.....
break;

case 'option2':
...
break;
.
.
default:
..
break;

}

here 'default' is executed if there is no match in above cases
ex:

prompt(i);

switch(i){
case 1:
...
break;
case 2:
....
break;

default:
..
break;
}

don't use {}, since we are already using break statement.  Here javascript will try to match the expression between the switch() paranthesis to each 'case', if not there it will evaluate to 'default'.

We also have logical operators just like in 'C' language

&& - for and operation
||     - for or operation
!     - it makes a true expression false


**toUpperCase() function will convert all the input to capital letters
ex: "this is".toUpperCase();     will give o/p 'THIS IS'

Algorithms lesson 1

Yesterday i read insertion sort from this book, 'Introduction to Algorithms' by cormen. Great book. Today i am executing in C programming language.

The main theory of insertion sort is that

for example if we have an array of unsorted numbers, our goal is sort by insertion sort.

so first we sort from the left, that means first we take 1 number to be sorted, next we make two numbers from left sorted, then 3 and so on till the length of the array to the right.

since 1st element is sorted if sorted array length is 1

The best way to explain insertion is by deck of cards. take n cards which are unsorted in your right hand.

we pick the left most card, and put it in the left hand.
the cards in the left hand are always sorted.

then we again pick the left most card from right hand and put it in left hand, such that the right hand cards are sorted. sometimes we have to shift the positions of the cards to the right, such that the smallest card is on the left most side
But in here, we are sort inside the array, so we are creating a subarray we finally after sorting spans the whole length of the array.

This is my C code:

#include <stdio.h>

int main(){
  int arr[6]={23,1,43,22,756,12};
  int i,j,k, key;

  for ( j =1;j<6; j++){
      key=a[j];                       // we are storing the left most element for the subarray
      i=j-1;                            // we are making 'j' as the length of subarray which is always sorted
      while( i>=0 && a[i]>key){              // we are entering the subarray here
         k = a[i];
         a[i] = a[i+1];                 // we are swapping left and right, since the left > right
         a[i+1]=k;
         i=i-1;
       }
       a[i+1] = key;               // when the loop exits we don't left element changed, so here we are changed
      }
   return 0;
}