-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy path9_some_every.js
91 lines (74 loc) · 1.67 KB
/
9_some_every.js
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
/*
Some is function which takes a callback function as input
It returns true as soon as the condition of the callback is satisfied
i.e if array is [5,7,9,11,15,13,10,31,21,24,54,100];
And condition inside the callback is if(value%2==0){return true}, then as soon as the callback is on 10 i.e first even number in this array it will return true.
In other words some will return true even if one element satisfies the condition inside the callback.
Whereas Every will return true only when every element satisfies the condition inside the callback. Else it will return false
*/
let array1 = [5,7,9,11,15,13,10,31,21,24,54,100];
let res1 = array1.some(function(value){
console.log(value);
if(value%2==0){
return true;
}
else{
return false;
}
});
console.log(res1);
/*
Output
5
7
9
11
15
13
10
true
*/
let array2 = [5,7,9,11,15,13,10,31,21,24,54,100];
let res2 = array2.every(function(value){
console.log(value);
if(value%2==0){
return true;
}
else{
return false;
}
});
console.log(res2);
/*
Since every returns true only when every element satisfies the condition inside callback. This is return a false on the very first element as it does not satisfy the condition.
Output
5
false
*/
let array3 = [2,22,222,12,152,134,106,3188,2142,24,54,100];
let res3 = array3.every(function(value){
console.log(value);
if(value%2==0){
return true;
}
else{
return false;
}
});
console.log(res3);
/*
Output
2
22
222
12
152
134
106
3188
2142
24
54
100
true
*/