-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArrayFunction.html
More file actions
94 lines (76 loc) · 2.94 KB
/
ArrayFunction.html
File metadata and controls
94 lines (76 loc) · 2.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<h1>Arrays Functions</h1>
<SCRIPT LANGUAGE="JavaScript">
var fruits = ['apple','orange','kiwi'];
alert (fruits[1]);
document.write("The Size of array \'fruits\' is : "+ fruits.length+'<br>');
document.write(fruits);
// add elements to end of array
fruits[3]='banana';
fruits[fruits.length]='cherry';
fruits.push('grape');
// add elements to begining of array
fruits.unshift('blackberry');
document.write('<br>'+"-------------------------------------------------"+'<br>')
// iteration tru arrays in js
document.write("The Array contents are: "+'<br>');
for(i=0;i<fruits.length;i++)
{
document.write(fruits[i]);
document.write('<BR/>');
}
document.write('<br>'+"-------------------------------------------------"+'<br>')
document.write("The Array contents after sorting : "+'<br>');
fruits.sort();
// for in loop -- iterate tru arrays/objects
var text='';
for(var i in fruits) //For In Loop
{
text+= fruits[i];
text+= '\n';
document.writeln(fruits[i]);
}
document.writeln("<br>"+text);
//removing elements
document.write('<br>'+"-------------------------------------------------"+'<br>')
document.write('<br>'+"The Array contents after removing some elements : "+'<br>')
fruits.pop(); // remove elemnts from end of array
fruits.shift(); // ,, ,, ,, begining ,,
for(i=0;i<fruits.length;i++)
{
document.write(fruits[i]);
document.write('<BR/>');
}
document.write('<br>'+"-------------------------------------------------"+'<br>')
var arr = [true, 2, 3, "4","Hello","hello"];
document.write('<br>'+"The Array contents before reversing elements : "+'<br>')
document.write('<br>'+arr);
document.write('<br>'+"The Array contents after reversing elements : "+'<br>')
new_array = arr.reverse()
document.write('<br>'+new_array);
document.write('<br>'+"-------------------------------------------------"+'<br>');
document.write('<br>'+"The Array contents after concatinating : "+'<br>');
new_array = arr.concat(5, 6, 7)
document.write('<br>'+new_array);
console.log(new_array);
//slice creates a copy of the array and extracts a subsequence of it.
document.write('<br>'+"-------------------------------------------------"+'<br>');
var arr1 = [23,56,87,32,75,13];
//Extracted array
var new_arr1 = arr1.slice(2,5);
document.write(arr1);
document.write("<br>");
document.write('<br>'+"The Array contents after slicing : "+'<br>');
document.write('<br>'+new_arr1);
console.log(new_array);
console.log("End");
</script>
</body>
</html>