Skip to content

Commit 9eaf5f5

Browse files
committed
added is_num_array method
1 parent 5700ef7 commit 9eaf5f5

File tree

1 file changed

+12
-1
lines changed

1 file changed

+12
-1
lines changed

index.js

Lines changed: 12 additions & 1 deletion
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);
@@ -126,12 +136,13 @@ class Helper {
126136
//To sort numeric arrays ascending and descending
127137
static sort_nums(arr, reverse = false) {
128138
this.is_array(arr);
139+
this.is_num_array(arr);
129140
if (reverse) {
130141
return arr.sort(function(a, b){return b - a});
131142
} else {
132143
return arr.sort(function(a, b){return a - b});
133144
}
134-
}
145+
}
135146
}
136147

137148
export default Helper;

0 commit comments

Comments
 (0)