-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharraysPracticeSet.html
More file actions
186 lines (139 loc) · 2.42 KB
/
arraysPracticeSet.html
File metadata and controls
186 lines (139 loc) · 2.42 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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
<script>
/ 1.
Combining 2 arrays
let arr1 = [1,2,3,4,5];
let arr2 = [6,7,8,9,10];
let combinedArray = [...arr1,...arr2];
console.log(combinedArray);
2.
reversing an Array
let arr1 = [1,2,3,4,5];
let ans = arr1.reverse();
console.log(ans);
3.
Finding out duplicate elements in an array
let findingDarray = (array) =>{
return array.filter((value,index)=>array.indexOf(value) !== index);
}
let arr1 = [1, 2, 3, 4, 5, 2, 7, 8, 2, 10, 1,20,20,40,90,90];
let ans = findingDarray(arr1);
4.
Splicing an Array
let arr = ["apples","mangoes","grapes","tomatoes"];
arr.splice(0,0,"litchi");
console.log(arr[0]);
4.1
Splicing
let arr = [1,2,3,4,5,6];
let ans = arr.splice(0,1,0);
console.log(ans);
This is an output based question the answer would be [1];
4.2
Splicing
let arr2 = [7,8,9,10];
arr2.splice(0,2,6);
for(let i in arr2){
console.log(arr2[i]);
}
Slicing
5
let str = "bbhargav";
console.log(str.slice(1,8));
5.1
let str1 = "DILLIROLEX";
console.log(str1.slice(5,10));
5.2
let str = "ANBUSANTHANAM";
console.log(str.slice(4,13));
forEach
6
let arr = [1,2,3,4,5];
arr.forEach((el)=>{
console.log( el*el );
});
6.1
let arr1 = [6,7,8,9,10] ;
arr1.forEach((el)=>{
console.log(el>7);
});
output:
false
false
true
true
true
6.2
let arr1 = [6,7,8,9,10];
arr1.forEach((el) => {
console.log(el / 2);
});
output:
3
3.5
4
4.5
5
6.3
let array = [20,34,56,78,96];
array.forEach((el)=>{
console.log(el * el);
});
output:
400
1156
3136
6084
9216
7
Map method
let array = [20,34,56,78,96];
array.map((val)=>{
console.log(val > 40);
});
output:
false
false
true
true
true
7.1
let array = [20,34,56,78,96];
array.map((el)=>{
console.log(el / 2 );
});
output:
10
17
28
39
48
8
Filter Method
let array = [20,34,56,78,96];
array.filter((el)=>{
console.log(el > 50);
});
output:
false
false
true
true
true
8.1
to get the values instead of booolean:-
let array = [20,34,56,78,96];
let result = array.filter((el)=>{
return el > 50 ;
});
console.log(result);
output:
[ 56, 78, 96 ]
8.2
let array = [20,34,56,78,96];
let res = array.filter((el)=>{
return el >= 30 ;
});
console.log(res);
output:
[ 34, 56, 78, 96 ]
</script>