CSS - Practical 3
CSS - Practical 3
3:
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;
}
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");
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.