0% found this document useful (0 votes)
21 views8 pages

Unit II Programs

programs

Uploaded by

sunnygaikwad4747
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views8 pages

Unit II Programs

programs

Uploaded by

sunnygaikwad4747
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Array: Adding Element in Array:

<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>

Combining an Array Element (join( ) and


concat( )method: Defining a Function
<html> <html>
<head> <title>Combining Array Elements into a <head>
String Demo</title> </head> <title>Defining a Function</title>
<body> </head>
<script type="text/javascript"> <body>
var arr=new <script type="text/javascript">
Array("Yellow","Green","Gray","Orange","White", function demoFun()
"Red","Blue","Pink","Purple"); {
document.write("Array Elements:<br>"); document.write("Statement of Function");
for(i=0;i<arr.length;i++) }
{ </script>
document.write(arr[i]+"<br>"); <script>
} demoFun(); //Function Call
document.write("<br>Demo of Join():<br>"); </script>
var str1=arr.join(''); </body>
document.write(str1); </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> }

</head> function fun2()


<body> {
<input type="button" value="Click here" document.write("This Statement by Func2()
onclick="fun1()"> <br>");
</body> }
</html> fun1();

Calling a Function Without Argument: </script>


<html> </body>
<head> </html>
<title>Demo of Calling a Function WithOut Changing case of String Upper to Lower
Argument</title> <html>
</head> <head>
<body> <title>Changing case of String Upper to
<script type="text/javascript"> Lower</title>
function addition() </head>
{ <body>
var a=100,b=200; <script>
var c=a+b; var str1="NAVARATRI";
document.write("Addition of A and B is:"+c); document.write("<br>Uppercase
} String:"+str1);
</script> var lower_Str=str1.toLowerCase();
<script type="text/javascript"> document.write("<br>Uppercase to
addition(); //calling a function Lowercase:"+lower_Str);
</script> </script>
</body> </body>
</html> </html>
Changing case of String Lowercase to Uppercase Converting String to a Number using Number():
<html> <html>
<head> <head>
<title>Changing case of String Lowercase to <title>Converting String to a Number using
Uppercase</title> Number()</title>
</head> </head>
<body> <body>
<script> <script type="text/javascript">
var str1="vijaya dashmi"; var str1="1000";
document.write("<br>Lowercase var num1=Number(str1); // conversion of
String:"+str1); string "1000" into a number
var Upper_Str=str1.toUpperCase(); var num2=1000; // declaration of number
document.write("<br>Lowercase to var result=num1+num2;
Uppercase:"+Upper_Str); document.write("The Result is:"+result);
</script> </script>
</body>
</html> </body>
Converting a Number to String </html>
<html>
<head>
<title>Converting a Number to String</title> Copying a Substring using substring:
</head> <html>
<body> <head>
<script type="text/javascript"> <title>Copying a Substring using
var num1=1000; substring</title>
var str1=num1.toString(); </head>
document.write("The value of Num1 <body>
is:"+num1); <script>
document.write("<br>The String str1 var str1="This is a CSS"
is:"+str1); var str2="Practical"
var result=num1+str1; var sub_str=str1.substr(10,3);
document.write("<br>The Result is:"+result); document.write("First String:"+str1);
</script> document.write("<br>Second String:"+str2);
</body> document.write("<br>Sub String
</html> is:"+sub_str);
var new_str=str2+" "+sub_str;
Converting String to a Number using parseInt() document.write("<br>New String
<html> is:"+new_str);
<head> <title>Converting String to a Number using
parseInt() </title> </head> </script>
<body> </body>
<script type="text/javascript"> </html>
var str1="1000"; //declaration of string
var num1=parseInt(str1); // conversion of
string "1000" into a number
var num2=1000; // declaration of number
var result=num1+num2;
document.write("The Result is:"+result);
</script> </body> </html>
Copying a Substring using substring Unicode of a Character charCodeAt() method
<html> <html>
<head> <title>Copying a Substring using <head>
substring</title> </head> <title>Unicode of a Character charCodeAt()
<body> method</title>
<script> </head>
var str1="This is a CSS" <body>
var str2="Practical" <script type="text/javascript">
var sub_str=str1.substring(10,13) var ch='A';
document.write("First String:"+str1); var unicode_char=ch.charCodeAt();
document.write("<br>Second String:"+str2); document.write("Unicode of 'A'
document.write("<br>Sub String is:"+unicode_char);
is:"+sub_str);
var new_str=str2+" "+sub_str; </script>
document.write("<br>New String </body>
is:"+new_str); </html>
</script> Retriving a Position of Character:
</body> <html>
</html> <head>
Dividing Text in a String: <title>Retriving a Position</title>
<html> </head>
<head> <body>
<title> Demo Dividing Text in a String </title> <script type="text/javascript">
</head> var str1="HHJB POLYTECHNIC";
<body> var str_pos=str1.indexOf("POLYTECHNIC");
<Script type="text/javascript"> document.write("The word 'POLYTECHNIC'
var str1="Shri HHJB Polytechnic"; in given string started at position:"+str_pos);
var arrList=str1.split(" "); </script>
document.write("<br>First:"+arrList[0]+"<br> </body>
Middel:"+arrList[1]+"<br>Last:"+arrList[2]); </html>
</Script> Retriving a Character from given Position
</body> <html>
</html> <head>
Character from Unicode using fromCharCode() <title>Retriving a Character from given
method Position</title>
<html> </head>
<head> <body>
<title>Finding Character from Unicode using <script type="text/javascript">
fromCharCode() method</title> var str1="HHJB POLYTECHNIC";
</head> var str_pos=str1.charAt(2);
<body> document.write("Character in a given string at
<script type="text/javascript"> 7th position:"+str_pos);
var num=97; </script>
var ch=String.fromCharCode(num); </body>
document.write("Character of Unicode '97':"+ch); </html>
</script>
</body>
</html>
Join a String method 1: Length of String
<html> <html>
<head> <head>
<title>Demo Join a String</title> <title>Demo of Length of String</title>
</head> </head>
<body> <body>
<script type="text/javascript"> <script type="text/javascript">
var str1="Jai "; var str="Client Side Scripting";
var str2="Mata di"; var len_str;
var str3=str1+str2; len_str=str.length;
document.write("After Joining two document.write("My string length
Strings:"+str3); is:"+len_str);
</script> </script>
</body> </body> </html>
</html>
Join a String method 2:
<html>
<head>
<title>Demo Join a String</title>
</head>
<body>
<script type="text/javascript">
var str1="Jai ";
var str2="Mata di";
var str3=" to all";
var str4=str1.concat(str2,str3);
var str5=str1.concat(" hai");
document.write("After Joining two
Strings:"+str4);
document.write("</br>After Joining two
Strings:"+str5);
</script>
</body>
</html>

You might also like