File tree Expand file tree Collapse file tree 1 file changed +12
-1
lines changed Expand file tree Collapse file tree 1 file changed +12
-1
lines changed Original file line number Diff line number Diff 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
137148export default Helper ;
You can’t perform that action at this time.
0 commit comments