Exp 3
Exp 3
3
Aim: Develop JavaScript to implement Array functionalities.
Theory:
Array:
It is a collection of more than one element value of similar data type. Array is used store multiple values
required for processing data. An array has user defined name long with index value to identify each element
stores in an array. A combination of array name and an index value is same as having a single variable.
Looping an array:
An array element can be accessed using an index variable. By using loop, we can traverse through all the
elements of an array by incrementing index variable by one, after every visit to an element.
In looping, a index variable/counter variable is initially assigned with 0 position. By using for loop or while
loop, each element from an array can be visited with index variable. Initially index variable will point to first
element stored at 0th position. Then index variable is incremented by one to point to next position and so on.
The loop will execute till all elements are visited and index variable points to length value.
Sorting An array:
Arranging all array elements in alphabetical order for strings and ascending order for numbers.
sort( ) function is used to perform sorting on array elements.
Syntax: array_name.sort( );
Syntax: array_name.push(“value”);
Combining array elements into a string:
1. concat( ) : It is used to merge all elements of an array into one string.
Syntax: var variable_name=arrayname.concat( );
Above syntax combines elements into one string and separate each element with a comma.
Programs:
Develop a JavaScript to declare an array with five elements and display all on a webpage.
<html>
<head></head>
<body>
<script type="text/javascript">
var arr=new Array ("book", "pen", "pencil", "eraser",
"notebook");
var i;
document. write ("Array Elements are: ");
for (i=0; i<arr.length;i++)
{
document. write("<br>" + arr[i]);
}
</script>
</body>
</html>
Develop a JavaScript code to declare an array with five names of flowers and display them in sorted
order.
<html>
<head></head>
<body>
<script type="text/javascript">
var flowers=new Array("Rose", "Jasmine", "Mogra",
"Lotus","Jai");
var i;
document.write("Array Elements are : ");
for(i=0;i<flowers.length;i++)
{
document.write("<br>" + flowers[i]);
}
flowers.sort();
document.write("<br>Sorted Elements are : ");
for(i=0;i<flowers.length;i++)
{
document.write("<br>" + flowers[i]);
}
</script>
</body>
</html>
Develop a JavaScript code to declare an array with five elements and display them in reverse order of
entry in an array.
<html>
<head></head>
<body>
<script type="text/javascript">
var flowers=new Array ("Rose", "Jasmine", "Mogra",
"Lotus", "Jai");
var i;
document. write ("Array Elements are: ");
for (i=0; i<flowers. length; i++)
{
document. write("<br>" + flowers[i]);
}
flowers.reverse();
document. write("<br><br>Array Elements in
reverse order are: ");
for (i=0; i<flowers. length; i++)
{
document. write("<br>" + flowers[i]);
}
</script>
</body>
</html>
Develop a JavaScript code to declare an array with three colors. Merge all array elements in one
string, separated with ‘,’ character and display the string.
<html>
<head></head>
<body>
<script type="text/javascript">
var arr=new Array("pink","blue","red");
var i;
document. write("Array elements are : ");
for(i=0;i<arr.length;i++)
{
document. write("<br>" + arr[i]);
}
var str=arr.concat();
document. write("<br>String = " + str);
</script>
</body>
</html>
Develop a JavaScript code to declare an array with three colors. Merge all array elements in one
string, separated with ‘&’ character and display the string.
<html>
<head></head>
<body>
<script type="text/javascript">
var arr=new Array("pink","blue","red");
var i;
document. write ("Array elements are: ");
for(i=0;i<arr.length;i++)
{
document. write("<br>" + arr[i]);
}
var str=arr.join('&');
document. write("<br>String = " + str);
</script>
</body>
</html>
Develop a JavaScript code to declare an array with three elements and Shift all elements from one
array to another array.
<html>
<body>
<script type="text/javascript">
var a=new Array("pen","pencil","book");
var b=new Array ();
var i;
document. write ("Elements of array a= ");
for (i=0; i<a.length;i++)
document. write("<br>" + a[i]);
for(i=a.length;i>0;i--)
{
b.push(a.shift());
}
document. write ("<br><br>Elements of array b= ");
for (i=0; i<b.length; i++)
document. write("<br>" + b[i]);
</script>
</body>
</html>
Practical Questions:
1. Write a JavaScript program to arrange all elements in descending order.
2. Write a JavaScript program to accept five numbers in an array and display them in sorted order.
3. Write a JavaScript program to copy one array into another array.
Conclusion:
With all the concepts based on array functionality, successfully developed and executed JavaScript codes
with correct output.