-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathArray_Functions_1.html
More file actions
113 lines (93 loc) · 3.81 KB
/
Array_Functions_1.html
File metadata and controls
113 lines (93 loc) · 3.81 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
<html>
<head>
<script type="text/javascript" >
//This is a comment
//----------------------------------------------------------------------------------------------------------
// Array.every() calls the callback function for each element of array until it finds an element for which the callback
// Function to check if all emements of array are smaller than 100 or not using array.every()
function fnIsLessThan100_Every(arr){
return arr.every(function(element){
return (element < 100);
});
}
fnIsLessThan100_Every([30,60,90]); // Output : true
fnIsLessThan100_Every([30,60,90,120]); // Output : false
//----------------------------------------------------------------------------------------------------------
// Function to check if all emements of array are smaller than 100 or not using for loop
function fnIsLessThan100_Loop(arr){
for(var i = 0 ; i < arr.length; i ++){
if(arr[i] >= 100){
return false;
}
}
return true;
}
fnIsLessThan100_Loop([30,60,90]); // Output : true
fnIsLessThan100_Loop([30,60,90,120]); // Output : false
//----------------------------------------------------------------------------------------------------------
// Function to check if all elements of array are of a spcified datatype using array.every()
function fnCheckDatatype_Every(arr, sDatatype){
return arr.every(function(element){
return typeof(element) === sDatatype;
},sDatatype)
}
fnCheckDatatype_Every(["Geeks","for","Geeks"],"string"); // Output : true
fnCheckDatatype_Every(["Geeks",4,"Geeks"],"string"); // Output : false
//----------------------------------------------------------------------------------------------------------
// Function to check if all elements of array are of a spcified datatype using for loop
function fnCheckDatatype_Loop(arr, sDatatype){
for(var i = 0 ; i < arr.length; i++){
if(typeof(arr[i]) !== sDatatype){
return false;
}
}
return true;
}
fnCheckDatatype_Loop(["Geeks","for","Geeks"],"string"); // Output : true
fnCheckDatatype_Loop(["Geeks",4,"Geeks"],"string"); // Output : false
//----------------------------------------------------------------------------------------------------------
//Function to check if array contains any number greater than 100
function fnIsGreaterThan100_loop(arr){
for(var i = 0 ; i < arr.length; i ++){
if(arr[i] > 100){
return true;
}
}
return false;
}
fnIsGreaterThan100_loop([30,60,90,120]); // Output : true
fnIsGreaterThan100_loop([30,60,90]); // Output : false
//----------------------------------------------------------------------------------------------------------
//Function to check if array contains any number greater than 100
function fnIsGreaterThan100_some(arr){
return arr.some(function(element){
return (element > 100);
});
}
fnIsGreaterThan100_some([30,60,90,120]); // Output : true
fnIsGreaterThan100_some([30,60,90]); // Output : false
//----------------------------------------------------------------------------------------------------------
// Function to check if array contains any even number
function fnIsEven_loop(arr){
for(var i = 0 ; i < arr.length ; i ++){
if(arr[i] % 2 === 0){
return true;
}
}
return false;
}
fnIsEven_loop([1,3,5]) // Output : false
fnIsEven_loop([1,3,5,6]) // Output : true
//----------------------------------------------------------------------------------------------------------
// Function to check if array contains any even number
function fnIsEven_some(arr){
return arr.some(function(element){
return (element %2 === 0);
});
}
fnIsEven_some([1,3,5]) // Output : false
fnIsEven_some([1,3,5,6]) // Output : true
//----------------------------------------------------------------------------------------------------------
</script>
</head>
</html>