-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathBasic2.js
More file actions
62 lines (47 loc) · 1.23 KB
/
Basic2.js
File metadata and controls
62 lines (47 loc) · 1.23 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
// Array -- Foreach, map, Filter
//Foreach is used to perform operations on every element of the arrays
{
let arr =[1,2,3,4,5,6,7];
arr.forEach(function(val){
console.log(val);
})
}
// Question1 . By use of foreach print hello in front of each elements of array
{
let arr = ['john','roman','batista']
arr.forEach(function(vals){
console.log(('Hello, '+ vals));
})
}
// Question2. Print all element of the array, but leave all those element who is greater than three
{
let arr = [1,2,3,4,5,3,5,12,1,3,1,4];
arr.forEach(function(e){
if(e<=3){
console.log(e);
}
})
//Filter Returns in Form of new Array
let newArr = arr.filter(function(e){
if(e<=3){
return e
}
})
console.log(newArr);
let sum = 0;
arr.forEach(function(val){
sum = sum + val;
})
console.log(sum);
}
//Find the Sum of array numerical element, skip if element is string
{
let arr = [1,2,3,'4',5,'abhishek']
let sum = 0;
arr.forEach(function(val){
if(typeof val === "number"){
sum += val;
}
});
console.log(sum);
}