@@ -71,10 +71,10 @@ function averageWordLength(arr){
71
71
}
72
72
73
73
let sumWords = 0 ;
74
- for ( let i = 0 ; i < arr . length ; i ++ ) {
75
- sumWords += arr [ i ] . length ;
74
+ for ( let i = 0 ; i < arr . length ; i ++ ) { // loop over array
75
+ sumWords += arr [ i ] . length ; // add number of characters of word in array to sumWords
76
76
}
77
- return sumWords / arr . length ;
77
+ return sumWords / arr . length ; // total of sumWords divided by length of array
78
78
}
79
79
80
80
@@ -93,9 +93,39 @@ const wordsUnique = [
93
93
'bring'
94
94
] ;
95
95
96
+ function uniquifyArray ( arr ) {
97
+ if ( ! arr . length ) { // check if array is empty
98
+ return null ; // if so return null
99
+ }
100
+
101
+ let newArr = [ ] ;
102
+ for ( let i = 0 ; i < arr . length ; i ++ ) {
103
+ if ( newArr . indexOf ( arr [ i ] ) === - 1 ) {
104
+ newArr . push ( arr [ i ] ) ;
105
+ }
106
+ }
107
+ return newArr ;
108
+ }
109
+
96
110
// Iteration #6: Find elements
97
111
const wordsFind = [ 'machine' , 'subset' , 'trouble' , 'starting' , 'matter' , 'eating' , 'truth' , 'disobedience' ] ;
98
112
113
+ function doesWordExist ( arr , word ) {
114
+ if ( ! arr . length ) { // check if array is empty
115
+ return null ; // if so return null
116
+ }
117
+
118
+ for ( var i = 0 ; i < arr . length ; i ++ ) { // loop over array
119
+ if ( arr [ i ] === word ) { // check if specific word is in array
120
+ return true ; // if so, return true
121
+ }
122
+ }
123
+ return false ; // word is not in array, return false
124
+ }
125
+
126
+ // doesWordExist(wordsFind, 'ajax'); //=> false
127
+ // doesWordExist(wordsFind, 'trouble'); //=> true
128
+
99
129
// Iteration #7: Count repetition
100
130
const wordsCount = [
101
131
'machine' ,
@@ -111,6 +141,23 @@ const wordsCount = [
111
141
'matter'
112
142
] ;
113
143
144
+ function howManyTimes ( arr , word ) {
145
+ if ( ! arr . length ) { // check if array is empty
146
+ return 0 ; // if so return null
147
+ }
148
+
149
+ let countWord = 0 ;
150
+ for ( var i = 0 ; i < arr . length ; i ++ ) { // loop over array
151
+ if ( arr [ i ] === word ) {
152
+ countWord ++ ;
153
+ }
154
+ }
155
+ return countWord ;
156
+ }
157
+
158
+ howManyTimes ( wordsCount , 'matter' ) ; //==> 4
159
+ howManyTimes ( wordsCount , 'ajax' ) ; //==> 4
160
+
114
161
// Iteration #8: Bonus
115
162
116
163
const matrix = [
0 commit comments