Unit II Programs
Unit II Programs
<html>
Array Declaration: <head>
<html> <title>Adding Array Element</title>
<head> </head>
<title>Array Declaration,Defination <body>
Demo</title> <Script type="text/javascript">
</head> var arr= new Array(10,30,5,43,22,90,80);
<body> document.write("Display Array list before Adding
<Script type="text/javascript"> Element<br>");
for(i=0;i<arr.length;i++) {
var arr= new Array(10,30,5,43,22,90,80);
document.write("Display Array list using document.write(arr[i]+"<br>");
method 1 <br>"); }
for(i=0;i<=6;i++) document.write("Display Array list after
{ adding element<br>");
document.write(arr[i]+"<br>"); arr[arr.length]=30;
} for(i=0;i<arr.length;i++)
var arr1=[10,20,30,40,50]; {
document.write("Display Array list using document.write(arr[i]+"<br>");
method 2 <br>"); }
for(i=0;i<=4;i++) </Script>
{ </body>
document.write(arr1[i]+"<br>"); </html>
}
</Script> Reverse Element in Array:
</body> <html>
</html> <head>
<title>Reverse Method Demo</title>
Array Looping: </head>
<html> <body>
<head> <script type="text/javascript">
<title>Looing an Array</title> var arr=new Array(9,5,4,0,2,6);
</head> document.write("Array Elements Before
<body> Reverse:<br>");
<script type="text/javascript"> for(i=0;i<arr.length;i++)
var arr=new Array(); {
arr[0]="Mahindra"; document.write(arr[i]+"<br>");
arr[1]="Suzuki"; }
arr[2]="Toyota"; arr.reverse();
arr[3]="Audi"; document.write("Array Elements After
arr[4]="BMW"; Reverse:<br>");
for(i=0;i<=4;i++) for(i=0;i<arr.length;i++)
{ {
document.write(arr[i]+"<br>"); document.write(arr[i]+"<br>");
} }
</script> </script>
</body> </body>
</html> </html>
Array unshift() method:
Sorting Element in Array: <html>
<html> <head> <title>unshift Method</title>
<head> </head>
<title>Sorting Array Elements</title> <body>
</head> <script type="text/javascript">
<body> var arr1= new Array(10,20,30,40,50);
<script type="text/javascript"> document.write("<br> Elements of Array
var arr=new Array(9,5,4,0,2,6); are:");
document.write("Array Elements Before for(i=0;i<arr1.length;i++)
Sort:<br>"); {
for(i=0;i<arr.length;i++) document.write("<br>"+arr1[i]);
{ }
document.write(arr[i]+"<br>"); document.write("<br> unshift() method:");
} arr1.unshift(12);
arr.sort(); document.write("<br> Elements of Array After
document.write("Array Elements After unshift() are:");
Sort:<br>"); for(i=0;i<arr1.length;i++)
for(i=0;i<arr.length;i++) {
{ document.write("<br>"+arr1[i]);
document.write(arr[i]+"<br>"); }
} </script> </body> </html>
</script>
</body> Array push() method:
</html> <html>
<head> <title>Demo of push() Method</title>
Array shift() method: </head>
<html> <body>
<head> <title>Demo of Shift Method</title> <script type="text/javascript">
</head> var arr1= new Array(10,20,30,40,50);
<body> document.write("<br> Elements of Array
<script type="text/javascript"> are:");
var arr1= new Array(10,20,30,40,50); for(i=0;i<arr1.length;i++)
document.write("<br> Elements of Array are:"); {
for(i=0;i<arr1.length;i++) document.write("<br>"+arr1[i]);
{ }
document.write("<br>"+arr1[i]); document.write("<br> Demo of push()
} method:");
document.write("<br> Demo of Shift() method:"); arr1.push(90);
var shft=arr1.shift(); document.write("<br> Elements of Array After
document.write("<br> Elements of Array After push() are:");
Shift() are:"); for(i=0;i<arr1.length;i++)
for(i=0;i<arr1.length;i++) {
{ document.write("<br>"+arr1[i]);
document.write("<br>"+arr1[i]); }
}
</script> </script>
</body> </body>
</html> </html>
Array pop() method: Object as associative Array:
<html> <html>
<head> <title>Demo of pop() Method</title> <head>
</head> <title>Object as Associative Array</title>
<body> </head>
<script type="text/javascript"> <body>
var arr1= new Array(10,20,30,40,50); <script type="text/javascript">
document.write("<br> Elements of Array are:"); var arr1= new Array(10,20,30,40,50);
for(i=0;i<arr1.length;i++) document.write("<br> Elements of Array:");
{ for(i=0;i<arr1.length;i++)
document.write("<br>"+arr1[i]); {
} document.write("<br>"+arr1[i]);
document.write("<br> Demo of pop() method:"); }
var a=arr1.pop(); document.write("<br> Object as Associative
document.write("<br> Elements of Array After Array:");
pop() are:"); for(i in arr1)
for(i=0;i<arr1.length;i++) {
{ document.write("<br>"+i+"=>"+arr1[i]);
document.write("<br>"+arr1[i]); }
}
document.write("<br><br><br>"+a+" is </script>
removed from array after execution of pop()."); </body>
</script></body></html> </html>
document.write("<br>Demo of
Concat():<br>");
var str2=arr.concat('');
document.write(str2);
</script> </body> </html>
Writing a Function Calling a Function With Argument
<html> <html>
<head> <head> <title>Demo of Calling a Function With
<title>writing a Function</title> Argument</title></head>
</head> <body>
<body> <script type="text/javascript">
<script type="text/javascript"> function addition(a,b)
function fun1() {
{ c=a+b;
document.write("This statement write in document.write("Addition of A and B is:"+c);
fun1().<br>"); }
} </script>
</script> <script type="text/javascript">
<script> var x=10,y=20;
fun1(); //function calling addition(x,y);
document.write("This statement write </script>
outside the fun1().<br>"); </body>
</html>
</script>
</body> Calling a Function From HTML method 1:
</html> <html>
Global variable and local variable: <head>
<html> <title> Demo of Calling a Function From
<head> HTML </title>
<title>Global variable Demo</title>
<body> <script type="text/javascript">
<script type="text/javascript"> function fun1()
var a=30; // global variable {
var b=20; // global variable document.write("This is A sample statement
function fun1() in Fun1().");
{ }
var a=10; // local variable of fun1() </script>
document.write("Value of a is="+a);
document.write("<br>Value of b is="+b); </head>
} <body onload="fun1()">
fun1(); // function call </body>
document.write("<br>Value of a is="+a); </html>
</script>
</body>
</head>
</html>
Calling a Function From HTML method 2: Function calling Another Function
<html>
<html> <head>
<head> <title>Demo of Function calling Another
<title> Demo of Calling a Function From Function</title>
HTML </title> </head>
<body>
<script type="text/javascript"> <script type="text/javascript">
function fun1() function fun1()
{ {
document.write("This is A sample statement document.write("This Statement by Func1()
in Fun1()."); <br>");
} fun2(); //calling function fun2() using fun1()
</script> }