1- //To throw error in case of invalid datatype.
1+ // To throw error in case of invalid datatype.
22function _throw ( ) {
33 throw new TypeError ( "Must be a valid array!" ) ;
44}
55
6- //To check if arr is array.
6+ // To check if arr is array.
77function is_array ( arr ) {
88 return Array . isArray ( arr ) ? true : _throw ( ) ;
99}
1010
11- //To get the head or first element of the array.
11+ // To get the head or first element of the array.
1212function head ( arr ) {
1313 is_array ( arr ) ;
1414 return arr [ 0 ] ;
1515}
1616
17- //To get the tail last element of the array.
17+ // To get the tail last element of the array.
1818function tail ( arr ) {
1919 is_array ( arr ) ;
2020 let element = arr . pop ( ) ;
2121 arr . push ( element ) ;
2222 return element ;
2323}
2424
25- //To check the existence of an element inside the array.
25+ // To check the existence of an element inside the array.
2626function in_array ( arr , value ) {
2727 is_array ( arr ) ;
2828 return arr . includes ( value ) ;
2929}
3030
31- //To split arrays into fixed size chunks.
31+ // To split arrays into fixed size chunks.
3232function array_chunk ( arr , chunk_size ) {
3333 is_array ( arr ) ;
3434 if ( typeof chunk_size != "number" ) {
@@ -50,7 +50,7 @@ function array_chunk(arr, chunk_size) {
5050 }
5151}
5252
53- //To filter out arrays based on datatypes.
53+ // To filter out arrays based on datatypes.
5454function array_filter ( arr ) {
5555 is_array ( arr ) ;
5656 arr = arr . filter ( ( e ) => {
@@ -59,7 +59,7 @@ function array_filter(arr) {
5959 return arr ;
6060}
6161
62- //To get the frequency of occurence of each unique element inside the array.
62+ // To get the frequency of occurence of each unique element inside the array.
6363function array_frequency ( arr ) {
6464 is_array ( arr ) ;
6565 let freq_obj = { } ;
@@ -73,19 +73,19 @@ function array_frequency(arr) {
7373 return freq_obj ;
7474}
7575
76- //To convert Objects into Arrays.
76+ // To convert Objects into Arrays.
7777function object_to_array ( obj ) {
7878 let temp = [ ] ;
7979 const entries = Object . entries ( obj ) ;
8080 entries . forEach ( ( ent ) => temp . push ( ent [ 1 ] ) ) ;
8181 return temp ;
8282}
8383
84- //To get indexes of all occurences of an element inside an array.
84+ // To get indexes of all occurences of an element inside an array.
8585function get_all_indexes ( arr , val ) {
8686 is_array ( arr ) ;
87- var indexes = [ ] , i ;
88- for ( i = 0 ; i < arr . length ; i ++ )
87+ var indexes = [ ] ;
88+ for ( let i = 0 ; i < arr . length ; i ++ )
8989 if ( arr [ i ] === val )
9090 indexes . push ( i ) ;
9191 return indexes ;
0 commit comments