Topic 2 CSS
Topic 2 CSS
Topic 2 CSS
For example –
Normally the first element in an array is stored at 0th location, however we can start storing the
element from any position.
TM
Technical Publications (2
- An up thrust for knowledge
- 1)
Client Side Scripting Language 2-2 Array, Function and String
Fig. 2.1.2
Fig. 2.1.3
Following is a simple JavaScript that illustrates the initialization of array by defining the array
elements.
JavaScript Document
<!DOCTYPE html>
<html>
<head>
<title>Array Demo</title>
</head>
<body>
<strong>
<script type="text/javascript">
a=new Array(5);//creation of array
for(i=0;i<5;i++)
{
a[i]=i;
document.write(a[i]+"<br>");//displaying array
}
document.write("Another way of initialization"+"<br>");
b=new Array(11,22,33,44,55);//creation of array
for(i=0;i<5;i++)
{
document.write(b[i]+"<br>");//displaying array
}
document.write("Yet another way of initialization"+"<br>");
Client Side Scripting Language 2-3 Array, Function and String
Ex. 2.1.1 : Write a JavaScript to define the array elements and to find the length of array.
Sol. : <!DOCTYPE html>
<html>
<head>
<title>Array Demo</title>
</head>
<body>
<script type="text/javascript">
a=new Array(11,22,33,44,55);//creation of array
var len=a.length
Client Side Scripting Language 2-4 Array, Function and String
Script Explanation : In above JavaScript, We have used length property to calculate length of the array.
This actually gives total number of elements in the array.
JavaScript Document
<!DOCTYPE html>
<html>
<head>
<title>for Loop Demo</title>
</head>
<body>
<script type="text/javascript">
Days=new Array();
Days[0]="Sunday";
Days[1]="Monday";
Days[2]="Tuesday";
Days[3]="Wednesday";
Days[4]="Thursday";
Days[5]="Friday";
Days[6]="Saturday";
for(i=0;i<Days.length;i++)
{
document.write(Days[i]+"<br>");
}
</script>
</body>
</html>
Client Side Scripting Language 2-5 Array, Function and String
Output
JavaScript Document
<!DOCTYPE html>
<html>
<head>
<title>for Loop Demo</title>
</head>
<body>
<script type="text/javascript">
a=new Array();
a[0]=10;
a[1]=20;
a[2]=30
a[3]=40
a[4]=50;
document.write("The elements in the array are...<br/>");
for(i=0;i<a.length;i++)
{
document.write(a[i]+" ");
}
document.write("<br/><br/>The element is added in the array...<br/>");
a[a.length]=60;
document.write("<br/>Now, The elements in the array are...<br/>");
for(i=0;i<a.length;i++)
{
document.write(a[i]+" ");
}
Client Side Scripting Language 2-6 Array, Function and String
</script>
</body>
</html>
Output
JavaScript Document
<!DOCTYPE html>
<html>
<head>
<title>Sorting Demo</title>
</head>
<body>
<script type="text/javascript">
a=new Array();
a[0]=40;
a[1]=30;
a[2]=10
a[3]=50
a[4]=20;
document.write("The elements in the array are...<br/>");
for(i=0;i<a.length;i++)
{
document.write(a[i]+" ");
}
document.write("<br/><br/><b>The array is sorted...</b><br/>");
a.sort();
Client Side Scripting Language 2-7 Array, Function and String
JavaScript Document
<!DOCTYPE html>
<html>
<head>
<title>Combing Array Demo</title>
</head>
<body>
<script type="text/javascript">
a=new Array();
a[0]="Red";
a[1]="Orange";
a[2]="Yellow"
a[3]="Green"
Client Side Scripting Language 2-8 Array, Function and String
a[4]="Blue";
a[5]="Indigo";
a[6]="Violet";
document.write("<h3> The join() method</h3>");
var str1=a.join(' ');
document.write(str1);
document.write("<h3> The concat() method</h3>");
var str2=a.concat();
document.write(str2);
</script>
</body>
</html>
Output
a[3]=40
a[4]=50
document.write("<h4> The shift() method</h4>");
var num=a.shift();
document.write(num);
document.write("<h4>The elements in array are ...</h4>");
for(i=0;i<a.length;i++)
document.write(a[i]+" ");
</script>
</body>
</html>
Output
a[2]=30
a[3]=40
a[4]=50
document.write("<h4>The elements in array are ...</h4>");
for(i=0;i<a.length;i++)
document.write(a[i]+" ");
document.write("<h4> Calling The push() method</h4>");
a.push(60);
document.write("<h4>The elements in array are ...</h4>");
for(i=0;i<a.length;i++)
document.write(a[i]+" ");
</script>
</body>
</html>
Output
a[0]=10
a[1]=20
a[2]=30
a[3]=40
a[4]=50
document.write("<h4>The elements in array are ...</h4>");
for(i=0;i<a.length;i++)
document.write(a[i]+" ");
document.write("<h4> Calling The pop() method</h4>");
var val=a.pop();
document.write("The element returned is "+val)
document.write("<h4>The elements in array are ...</h4>");
for(i=0;i<a.length;i++)
document.write(a[i]+" ");
</script>
</body>
</html>
Output
<body>
<script type="text/javascript">
a=new Array();
a[0]=10
a[1]=20
a[2]=30
a[3]=40
a[4]=50
document.write("<h4>The elements in array are ...</h4>");
for(i=0;i<a.length;i++)
document.write(a[i]+" ");
document.write("<h4> Calling The reverse() method</h4>");
a.reverse();
document.write("<h4>The elements in array are ...</h4>");
for(i=0;i<a.length;i++)
document.write(a[i]+" ");
</script>
</body>
</html>
Output
JavaScript Document
<!DOCTYPE html>
<html>
<head>
<title>Associative Array Demo</title>
</head>
<body>
<script type="text/javascript">
a=new Object();
a["one"]=1;
a["two"]=2;
a["three"]=3;
a["four"]=4;
a["five"]=5;
document.write("<h4>The elements in array are ...</h4>");
for(i in a)
document.write(i+"="+a[i]+"<br/>");
</script>
</body>
</html>
Output
2.2 Function
We can write the functions in the JavaScript for bringing the modularity in the script.
Separate functions can be created for each separate task. This ultimately helps in finding the bug from
the program efficiently.
Client Side Scripting Language 2 - 14 Array, Function and String
JavaScript[FunDemo.html]
<!DOCTYPE html>
<html>
<head>
<title>Function Demo</title>
<script type="text/javascript">
function my_fun()
{
document.write("This statement is within the function"); Definition of function
}
</script>
</head>
<body>
<center>
<script type="text/javascript">
document.write("This statement is before a function call");
document.write("<br>");
my_fun(); // call to the function
</script>
</center>
</body>
</html>
Client Side Scripting Language 2 - 15 Array, Function and String
Output
Script Explanation
The above code is pretty simple. We have defined one function named my_fun in the head section of the
HTML document and called it from the body section. The corresponding write statements are used to
display the messages on the browser window.
Local Scope
If a variable is defined inside a function then that variable is a local variable and its scope is a local scope.
That also means, that the local variable is accessible only within a function in which it is defined. It is not
accessible outside that function.
Following program shows the use of local variable.
JavaScript Example for Demonstrating scope of Local Variable
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript">
function A()
{
Client Side Scripting Language 2 - 16 Array, Function and String
Global Variable
A variable is called global variable , if it is defined outside the function. The variable having global scope
is accessible by any function.
Following example illustrates the use of global variable.
{
document.getElementById("id2").innerHTML
="From function B(): a = "+a Output
}
</script>
</head>
<body>
<p id="id1"></p>
<p id="id2"></p>
<script type="text/javascript">
A()
B()
</script>
</body>
</html>
JavaScript Document
<!DOCTYPE html>
<html> Output
<head>
<script type="text/javascript">
function add()
{
var a=10
var b=20
c=a+b
document.write("Addition = "+c)
}
</script>
</head>
<body>
<h4> Function without passing argument</h4>
<script type="text/javascript">
add()
</script>
</body>
</html>
JavaScript Document
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript">
function A()
{
alert("Inside the function A");
}
</script>
</head>
<body onload="A()">
</script>
</body>
</html>
Client Side Scripting Language 2 - 19 Array, Function and String
Output
Output
JavaScript[FunRetDemo.html]
<!DOCTYPE html>
<html>
<head>
<title>Function Demo</title>
<script type="text/javascript">
function my_fun()
{
str="I am function returned value";
return str;
}
</script>
</head>
<body>
<center>
<script type="text/javascript">
document.write("This statement is before a function call");
document.write("<br>");
document.write("<h3>"+my_fun()+"<h3>");
</script>
</center>
</body>
</html>
Client Side Scripting Language 2 - 21 Array, Function and String
Output
2.4 String
String is a collection of characters.
Some commonly used methods of string object are concatenating two strings, converting the string to
upper case or lower case, finding the substring of a given string and so on.
The string is written within the quotes- either single quote or double quote. For example –
var myname=’ABC’
Method Meaning
concat(str) This method concatenates the two strings. For example s1.concat(s2) will result in
concatenation of string s1 with s2.
charAt(index_val) This method will return the character specified by value index_val.
substring(begin,end) This method returns the substring specified by begin and end character.
toLowerCase() This function is used to convert all the uppercase letters to lower case.
toUpperCase() This function is used to convert all the lowercase letters to upper case.
There is one important property of string object and that is length. For example
var my_str=”Hello”;
Length of the string “Hello” will be
var len; stored in the variable len
len=my_str.length;
Client Side Scripting Language 2 - 22 Array, Function and String
JavaScript Document
<!DOCTYPE html>
<html>
<script type="text/javascript">
var str="ABC"
alert("The character at first position of string: "+str+" is: "+str.charAt(0))
</script>
</body>
</html>
Client Side Scripting Language 2 - 23 Array, Function and String
Output
JavaScript Document
<!DOCTYPE html>
<html>
<script type="text/javascript">
var Name="Shivaji Shahaji Bhosale"
var list=Name.split(' ')
alert("First Name: "+list[0]+"\nMiddle Name: "+list[1]+"\nLast Name: "+list[2])
</script>
</body>
</html>
Output
1. Use of substring
The syntax of substring is
substring(start, end)
where start indicates the starting index and end indicates the ending index for extracting substring.
Client Side Scripting Language 2 - 25 Array, Function and String
Following program shows use of substring() function for retrieving the substring and then copying it to
some other string at the end.
<!DOCTYPE html>
<html>
<script type="text/javascript">
var Name1="Shivaji Shahaji Bhosale"
var Name2="Sambhaji"
var sub_str=Name1.substring(16,23)
document.write("<br/>The Name1: "+Name1)
document.write("<br/>The Surname: "+sub_str)
document.write("<br/>The Name2: "+Name2)
document.write("<br/><b>Copying the surname for Name2...</b>")
var Name3=Name2+" "+sub_str
document.write("<br/>The Name3: "+Name3)
</script>
</body>
</html>
Output
2. Use of substr
The substr method is also used to extract the substring from the text. The syntax for substr is as follows
substr(start,length)
where start indicates the starting index and length indicates the number of characters to be extracted.
Following example shows how to extract some substring using substr() method and then copy this
substring to another text.
JavaScript Document
<!DOCTYPE html>
<html>
<script type="text/javascript">
var Name1="Shivaji Shahaji Bhosale"
Client Side Scripting Language 2 - 26 Array, Function and String
var Name2="Sambhaji"
var sub_str=Name1.substr(16,7)
document.write("<br/>The Name1: "+Name1)
document.write("<br/>The Surname: "+sub_str)
document.write("<br/>The Name2: "+Name2)
document.write("<br/><b>Copying the surname for Name2...</b>")
var Name3=Name2+" "+sub_str
document.write("<br/>The Name3: "+Name3)
</script>
</body>
</html>
Output
Output
Using toUpperCase() method we can convert the small letters to capital letters.
For example –
<!DOCTYPE html>
<html>
<script type="text/javascript">
var str="welcome friends";
var newstr=str.toUpperCase()
document.write("<br/>The "+str+" is converted to: "+newstr)
</script>
</body>
</html>
Client Side Scripting Language 2 - 29 Array, Function and String
Output