1
+ /**
2
+ * The some() method tests whether at least one element in the array passes the test implemented by the provided function.
3
+ * It returns true if, in the array, it finds an element for which the provided function returns true;
4
+ * otherwise it returns false. It doesn't modify the array.
5
+ */
6
+ const log = console . log
7
+ log ( )
8
+ const array = [ 1 , 2 , 3 , 4 , 5 ]
9
+
10
+ // checks whether an element is even
11
+ const even = ( element ) => element % 2 === 0
12
+ log ( 'checks whether an element is even' , array . some ( even ) )
13
+ // expected output: true
14
+ log ( )
15
+ // Testing value of array elements
16
+
17
+ const arr = [ 2 , 5 , 8 , 1 , 4 ]
18
+ const set = [ ( 12 , 5 , 8 , 1 , 4 ) ]
19
+ function isBiggerThan10 ( element , index , array ) {
20
+ return element > 10
21
+ }
22
+ log ( 'Testing value of array elements' , arr . some ( isBiggerThan10 ) ) // false
23
+ log ( 'Testing value of array elements' , set . some ( isBiggerThan10 ) ) // true
24
+
25
+ // Testing array elements using arrow functions
26
+ log ( )
27
+ log (
28
+ 'Testing array elements using arrow functions' ,
29
+ set . some ( ( x ) => x > 10 )
30
+ ) // false
31
+ log (
32
+ 'Testing array elements using arrow functions' ,
33
+ arr . some ( ( x ) => x > 10 )
34
+ ) // true
35
+ log ( )
36
+ // Checking whether a value exists in an array
37
+ const fruits = [ 'apple' , 'banana' , 'mango' , 'guava' ]
38
+
39
+ function checkAvailability ( arr , val ) {
40
+ return arr . some ( function ( arrVal ) {
41
+ return val === arrVal
42
+ } )
43
+ }
44
+
45
+ log (
46
+ 'Checking whether a value exists in an array' ,
47
+ checkAvailability ( fruits , 'kela' )
48
+ ) // false
49
+ log (
50
+ 'Checking whether a value exists in an array' ,
51
+ checkAvailability ( fruits , 'banana' )
52
+ ) // true
53
+
54
+ // Checking whether a value exists using an arrow function
55
+
56
+ function checkAvailabilityArrow ( arr , val ) {
57
+ return arr . some ( ( arrVal ) => val === arrVal )
58
+ }
59
+
60
+ log (
61
+ 'Checking whether a value exists in an array' ,
62
+ checkAvailabilityArrow ( fruits , 'kela' )
63
+ ) // false
64
+ log (
65
+ 'Checking whether a value exists in an array' ,
66
+ checkAvailabilityArrow ( fruits , 'banana' )
67
+ ) // true
68
+ log ( )
69
+ // Converting any value to Boolean
70
+ const TRUTHY_VALUES = [ true , 'true' , 1 ]
71
+ function getBoolean ( value ) {
72
+ 'use strict'
73
+
74
+ if ( typeof value === 'string' ) {
75
+ value = value . toLowerCase ( ) . trim ( )
76
+ }
77
+
78
+ return TRUTHY_VALUES . some ( function ( t ) {
79
+ return t === value
80
+ } )
81
+ }
82
+
83
+ log ( 'Converting any value to Boolean' , getBoolean ( false ) ) // false
84
+ log ( 'Converting any value to Boolean' , getBoolean ( 'false' ) ) // false
85
+ log ( 'Converting any value to Boolean' , getBoolean ( 1 ) ) // true
86
+ log ( 'Converting any value to Boolean' , getBoolean ( 'true' ) ) // true
87
+ log ( )
0 commit comments