0% found this document useful (0 votes)
37 views

CSS - Practical 3

The document discusses JavaScript array methods and functionalities including declaring, initializing, accessing, and looping through arrays. It also covers built-in array methods like sort, reverse, join, pop, shift, splice, slice and examples of using map and filter. The exercises section includes writing programs to perform array operations and sorting an array of subject marks.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
37 views

CSS - Practical 3

The document discusses JavaScript array methods and functionalities including declaring, initializing, accessing, and looping through arrays. It also covers built-in array methods like sort, reverse, join, pop, shift, splice, slice and examples of using map and filter. The exercises section includes writing programs to perform array operations and sorting an array of subject marks.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Practical No.

3:

I.Aim: Develop JavaScript to implement array functionalities.

II.Minimum Theoretical Background

A. Declaring & Initialising Array


Syntax : name_of_array = new Array(size_of_array)
Eg : scores = new Array(4);
Assigning values to array
To assign a value to the array, you use an index in brackets. Indexes begin with 0
scores[0] = 39;
scores[1] = 40;
scores[2] = 100;
scores[3] = 49;
Initialising an array
An array can be initialised at the time of declaration also.
scores = new Array(39,40,100,49);
In JavaScript 1.2 and later, you can also use a shorthand syntax to declare an array and
specify its contents.
The following statement is an alternative way to create the scores array:
scores = [39,40,100,49];

B. Accessing array elements


Element s are accessed using index
scores[0] = 39; //sets element 39 at location 0
//following statement stores the elements at location 0,1,2 in a variable scoredisp
scoredisp = “Scores: “ + scores[0] + “,” + scores[1] + “,” + scores[2];
document.write(scoredisp);

C. Looping an Array An array can be looped in 2 basic ways.


1. using standard loops
cars = {" alto", " verna" , " city" , "bolero" }
var i; for (i = 0; i < cars.length; i++)
{
document.write(cars[i] + "");
}
2. using forEach()
var txt = "";
var numbers = [45, 4, 9, 16, 25];
var over18=numbers.forEach(myFunction);
function myFunction(value, index, array)
{
txt = txt + value + " ";
}

Along with these, 2 more methods can be used as:


1.using Array.map()

The map() method creates a new array by performing a function on each array element.
The map() method does not execute the function for array elements without values.
The map() method does not change the original array.
var numbers1 = [45, 4, 9, 16, 25];
var numbers2 = numbers1.map(myFunction);
function myFunction(value)
{
return value * 2;
}

2. using Array.filter()
The filter() method creates a new array with array elements that passes a test.
var numbers = [45, 4, 9, 16, 25];
var over18 = numbers.filter(myFunction);
function myFunction(value, index, array)
{
return value > 18;
}

D. Methods used with Arrays

1. Sort: This method is used to sort the array elements


var fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.sort();

2. Reverse: This method is used to reverse an array


var fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.sort();
fruits.reverse();
3. Join: This method returns the array as a string. The elements will be separated by a
specified separator. The default separator is comma (,).
var fruits = ["Banana", "Orange", "Apple", "Mango"];
var energy = fruits.join(" and ");
o/p Banana and Orange and Apple and Mango
4. Pop:It removes the last element from an array
var fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.pop(); // Removes the last element ("Mango") from fruits
5. Shift: This method removes the first array element and "shifts" all other elements to a
lower index.
var fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.shift(); // Removes the first element "Banana" from fruits

6. Delete is used to delete element from array


var fruits = ["Banana", "Orange", "Apple", "Mango"];
delete fruits[0]; // Changes the first element in fruits to undefined

7. Splice : This method can be used to add new items in between to an array:
var fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.splice(2, 0, "Lemon", "Kiwi");
New Array: Banana,Orange,Lemon,Kiwi,Apple,Mango

III. Result :
………………………………………………………………………………………………………
………………………………………………………………………………………………………
………………………………………………………………………………………………………
IV. Conclusion(s)
………………………………………………………………………………………………………
………………………………………………………………………………………………………
………………………………………………………………………………………………………
V. Questions:-
1. Explain the array with an example.
-An array is a numbered group of data items that you can treat as a single unit.
- For example, you might use an array called scores to store several scores for a game.
- Arrays can contain strings, numbers, objects, or other types of data.
- Each item in an array is called an element of the array
Syntax : name_of_array = new Array(size_of_array)
Eg : scores = new Array(4);

2. Explain any 4 methods that can be used with arrays for adding elements.
1. Push : It is used for adding elements at end of array
var fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.push("Kiwi");

2. Unshift :It is used for adding elements at beginning of array


var list = ["foo", "bar"];
list.unshift("baz", "qux");
o/p: ["baz", "qux", "foo", "bar"]

3. Concat: it is used for adding array to another array


var list = ["foo", "bar"];
var newlist = list.concat( ["baz", "qux"] ); ["foo", "bar", "baz", "qux"] // newlist result
4. Add an Array Element at a Particular Index
var list = ["foo", "bar"];
list[2] = "baz"; // add element "baz" to the index position 2 in the array
list[3] = "qux";
o/p: foo, bar, baz, qux

3. Explain splice and slice methods. Also state the difference between them.
Splice: it is used for adding elements in between of an array.
var list = ["foo", "bar"];
list.splice( 1, 0, "baz"); // at index position 1, remove 0 elements, then add "baz" to that
position ["foo", "baz", "bar"]

Slice: This method slices out a piece of an array into a new array. The slice() method creates
a new array. It does not remove any elements from the source array.
var fruits = ["Banana", "Orange", "Lemon", "Apple", "Mango"];
var citrus = fruits.slice(1);
o/p Orange,Lemon,Apple,Mango

VI.Exercise:-
1. Write a program to perform all the array operations.
- Write the programs which are performed in a practical session.
2. Write a program to Accept the marks of 10 subjects from the user and store it in an array.
Sort them and display
- Write the programs which are performed in a practical session.

You might also like