Tuesday, 14 May 2013

Using Variables

Authour:I will teach you courses online

C Language C++ Asp.net VB.net Oracle Hardware and Networking MCSE
Contact: Name: Shrinivas Mobileno:919703619391 E-mail: vas.shrinivas002@gmail.com
About Variables:

Let us come to real world today you purchased a BAT or TV. You need a place to put these objects in your house. Meaning these objects are occupying some place in your house.

 Like this assume that BAT equals to 5 AND TV equals to 6 now today you purchased these numbers in market. Then you went to your home. You need a place to put this numbers in your computer so decided to put 5 in a and 6 in b. Now it's look like below

a=5 b=6

In java script to use any variable they use var keyword before variable. You can omit var in many cases. To understand where to declare a variable, we need to understand the concept of scope. 

If you declare a variable within the function, It will available only for function they are created in.

student=1;

Above is called local variable. Within the function you can use this variable
If you declare a variable outside of function or declared in main script, You can use throughout the script.

var student=1;


Assigning Values to Variables:

You all ready now that assigning values to variable see above

a=5 b=6 example

Increment procedure:

a=a+1;

a contains 5
a=5;

Above works from right to left, Not understood see this

first 5+1-->6

now a contains 6;

Equivalent of a=a+1 is a +=1;

Equivalent of a=a-1 is a -=1;

If u feel it's hard you can use this a++; for increment

a--; for subtraction

Operators in java script:

+ for Concatenate

+ Add
-  Subtract
* Multiply
/ Divide
% remainder
++ Increment
-- Decrement


Operator Precedence:

When you use more than one operator in an expression, JavaScript uses rules of operator precedence to decide how to calculate the value.

See example below

result=2+5*3;

above will give two results 1). 21-----2+5:7*3=21

                                      2). 17-----5*3:15+2=17


Which one is correct !!! 
In above situation java Script uses RULES. It will give higher precedence to multiplication then addition.

First multipication:5*3=15
second addition:2+15=17

See another problem

a+b+c+d/4

Above you think it will add a+b+c+d after that it will divided by 4

You think wrong:

Divided by gives higher precedence, D value is divide by four then add with other 3 numbers a,b,c;

solution for above problem is:

result=(a+b+c+d)/4;

Parentheses ensure that the four variables are added first, and then the sum is divided by four.

Data Types in JavaScript:

Numbers: Javascript supports both Integers[3,4,5] and floating point numbers[1.2333]

String: One or more characters of text is called String "I am great"

Boolean: These can have one of two values: true or false. More later

null value: Represented by the keyword null.

Converting Between Data Types


JavaScript handles conversions between data types for u whenver it can for example:

total=34;
document.write("The total is" + total);

Above print The total is 40, Because document.write function works with strings.
The JavaScript interpeter automatically converts non strings to string before performing the function.

Note: Did u understand above Interpeter works first.

But some times you will get problems

total=34 comes after 

avg=total/3;

What to do now?????

Because total is not number it's both integer and string.

We need to convert it to a regular numeric variable.

Js(JavaScript)contains two functions for this

parseInt()-Convert a string to an integer number.

parseFloat()-Converts a string to a floating point number.


Both of the functions reads numeric from string and return number.

stringvar="25 says bye";

numvar=parseInt(stringvar);


Above parseInt reads numeric from string "stringvar", it contains 25. Now 25 will return to numvar.

store=numvar/5; Now no problem with numvar contains 25.

contd .....

Creating a String object

There are two ways to create a new String object. The first is the one u know.Second is object oreinted.

store="This is only a example";

store = new String("This is only a example");


Example on Assigns values to Strings and Combining them
test1="Welcome to ";

test2="ABC pvt Ltd.,";

combined=test1+test2;


U can easily understand test1, test2 then what is combined variable. I have combined test1 and test2 with "+"[concatenation].Assigned into combined variable

copy code from below
----------------------------------------------------------------------------------------------------------
<html>

<head>

<title>String Test</title>

</head>


<body>

<h1>String Test </h1>

<script type="text/javascript">

test1="Welcome to ";

test2="ABC pvt Ltd.,";

combined=test1+test2;

alert(combined);

</script>

</body>

</html>
---------------------------------------------------------------------------------
Calculating string length

To count string length, We use length property

explanation on length:

tot="A leader will help society";

document.write(tot.length);

Example on length:

<html>
<head>

<title>to count lenght of string </title> </head>

<body>



<script type="text/javascript">


test="A Leader all ways work for society";

document.write(test.length);


</script>


</body>

</html>
--------------------------------------------------------------------------
Converting the String's Case:
To methods of the String object enables to convert string to lower or uppercase.

toUpperCase()-converts all characters to uppercase.

toLowerCase()-converts all characters to lowercase.

<html>
<head>

<title> Convert to uppercase </title>  </head>


<body>

<script type="text/javascript">

test="a man can do anything";


document.write(test.toUpperCase());


</script>



</body>

</html>
-------------------------------------------------------------------------------------------
Replace toUpperCase with toLowerCase, for lowercase.


Working with Substrings

Sometimes you want to read 2 or more letter from a string. then you can use Substring method.

test="Internetexplorer";

document.write(test.substring(3,6));

Count starts from 0(zero) left letter 3 that is [e] return up to 6th letter: ern
------------------------
Getting a single Character:

charAt method is a simple way to grab a single character from a string.

ex: arrived

alpha.charAt(0) returns a

alpha.charAt(5) returns e

Finding a Substring


In a given string you want to search for a particular string Like below
Ex:My name is not what u expect my name is shrinivas
Another use for substrings is to find a string within another string. One way is indexOf method.

test=” My name is not what u expect my name is shrinivas”;
loc=test.indexOf(“shrinivas”);
Now the value returned in the loc variable is and index into string. You that first character of the string is index 0(zero).

Now you understood everything. If multiple occurrence of strings what you will do???

Ex:My name is not what u expect my name is shrinivas

Like ex: Occurrence of name 2 times.

loc=temp.indexOf("name",3);


Use of second parameter is to search for multiple occurrences of string. After found the first string

Numeric Arrays:

Array: Array means, Today u decided to multiply 5 with these numbers 25,56,78,90.

In a ordinary program you take 4 variables (a=25, b=56, c=78, d=90) to multiply with 5
  Instead of declare 4 variables individually, you can use arrays.

<html>
<head>
<title>Multiplecation with 5 </title> </head>


<body>
<h1>Multiplecation with 5<h1>
<script type="text/javascript">

a=new Array(3);
b=5;

a[0]=25;
a[1]=56;
a[2]=78;
a[3]=90;

for(i=0;i<=3;i++)

{

a[i]=b*a[i];

document.write(a[i]+"<BR>");


}


</script>

</body>

</html>



You might be ask what is the advantage of taking arrays.
see if i take a=25, b=56, c=78, d=90 four variables
Advantages of useing ARRAYS:

1).These four variables store in different locations[Not a contiguous], It will take a little more time to fetch data. If you use ARRAYS process is very fast[Because data in contiguous location].

2).U can store multiple properties like Author: a[o]="Author FirName";
                                                                       a[1]="Author Sir name";
                                                                        a[2]="Phone number";
                                                                        a[3]="Street name";  
                                                                         a[4]="State";
                                                                         a[5]="Dist";




Accessing Array Elements:
a[0]=25;
a[1]=56;
a[2]=78;
a[3]=90;


for(i=0;i<=3;i++)

{



document.write(a[i]+"<BR>");


}

In above code :

First iteration zero will come a[0] then print 25

Second iteration 1 will come a[1] then print 56

third iteration 2 will come a[2] then print 78

so onnnnnnnnnnnnn...
copy code from below


<html>
<head>
<title>Contents of array </title> </head>


<body>
<h1>Contents of array<h1>
<script type="text/javascript">

a=new Array(3);
b=5;

a[0]=25;
a[1]=56;
a[2]=78;
a[3]=90;

for(i=0;i<=3;i++)

{



document.write(a[i]+"<BR>");


}


</script>

</body>

</html>


Creating a String Array: You can declare string array like numeric array.



fullnames= new Array(30);


You can assign string values.

fullnames[0]="Taj Mahal";
fullnames[1]="India is a great place";
fullnames[2]="civilization started their";

Splitting a String:


name="Great Vas Shrinivas";
chop=name.split(" ");

In this example split function splits the name string into three arrays.

chop[0]="Great"
chop[1]="Vas"
chop[2]="Shrinivas"


Opposite to split is join:Meaning you can joing array into one string.

fullname=chop.join(" ");



Sorting Numeric array: Sorting is my interesting topic.

Below example javascript uses

function numcompare(a,b)
{

return a-b;

}

nums = new Array(30,10,200,4);

sortorder=nums.sort(numcompare);



Output:4,10,30,200

Above is ordinary you can implement your own sorting by understanding my examples

First understand this

http://javascriptwithshrinivas.blogspot.com/2013/05/how-to-implement-sorting.html
Next try to understand this, It's very easy.


http://javascriptwithshrinivas.blogspot.com/2013/05/my-own-sorting.html



Above code you can change as per your requirements.












No comments:

Post a Comment