Skip to content

Commit 937cc1c

Browse files
authored
Merge pull request #11 from Nisarg03/main
New methods: sort_nums and is_num_array
2 parents 2befaa4 + 9eaf5f5 commit 937cc1c

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

index.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,16 @@ class Helper {
99
return Array.isArray(arr) ? true : this._throw();
1010
}
1111

12+
// To check if arr has only nums.
13+
static is_num_array(arr) {
14+
var a = arr.reduce(function(result, val) {
15+
return result && typeof val === 'number';
16+
}, true);
17+
if (a == false) {
18+
throw new TypeError("Must be an array of numbers!")
19+
}
20+
}
21+
1222
// To get the head or first element of the array.
1323
static head(arr) {
1424
this.is_array(arr);
@@ -123,6 +133,16 @@ class Helper {
123133
throw new TypeError("Input parameters not valid!");
124134
}
125135
}
136+
//To sort numeric arrays ascending and descending
137+
static sort_nums(arr, reverse = false) {
138+
this.is_array(arr);
139+
this.is_num_array(arr);
140+
if (reverse) {
141+
return arr.sort(function(a, b){return b - a});
142+
} else {
143+
return arr.sort(function(a, b){return a - b});
144+
}
145+
}
126146
}
127147

128148
export default Helper;

0 commit comments

Comments
 (0)