@@ -31,9 +31,8 @@ function sumNumbers(arr) {
31
31
return 0 ;
32
32
}
33
33
34
- sum ( arr ) ;
35
- return total ;
36
- /*
34
+ //sum(arr);
35
+ //return total;
37
36
38
37
let total = 0 ;
39
38
@@ -42,7 +41,6 @@ function sumNumbers(arr) {
42
41
}
43
42
return total ;
44
43
}
45
- */
46
44
47
45
// Iteration #3 BONUS
48
46
@@ -58,9 +56,11 @@ function sum(arr) {
58
56
if ( typeof arr [ i ] === "number" ) {
59
57
total += arr [ i ] ;
60
58
} else if ( typeof arr [ i ] === "boolean" ) {
61
- total += total + arr [ i ] ;
59
+ total += arr [ i ] ;
62
60
} else if ( typeof arr [ i ] === "string" ) {
63
61
total += arr [ i ] . length ;
62
+ } else if ( typeof arr [ i ] === "object" ) {
63
+ throw "Error: Unsupported data type sir or ma'am" ;
64
64
}
65
65
}
66
66
return total ;
@@ -76,14 +76,54 @@ function averageNumbers(arr) {
76
76
return null ;
77
77
}
78
78
79
- let average = sum ( arr ) / numbers . length ;
79
+ let average = sum ( arr ) / arr . length ;
80
80
81
81
return average ;
82
82
}
83
83
84
84
// Level 2: Array of strings
85
85
const wordsArr = [ 'seat' , 'correspond' , 'linen' , 'motif' , 'hole' , 'smell' , 'smart' , 'chaos' , 'fuel' , 'palace' ] ;
86
86
87
+ function averageWordLength ( arr ) {
88
+
89
+ if ( arr . length === 0 ) {
90
+ return null ;
91
+ }
92
+
93
+ let total = 0 ;
94
+
95
+ for ( let i = 0 ; i < arr . length ; i ++ ) {
96
+ total += arr [ i ] . length ;
97
+ }
98
+ let average = total / arr . length ;
99
+
100
+ return average ;
101
+ }
102
+
103
+ function avg ( arr ) {
104
+
105
+ if ( arr . length === 0 ) {
106
+ return null ;
107
+ }
108
+
109
+ let total = 0 ;
110
+
111
+ for ( let i = 0 ; i < arr . length ; i ++ ) {
112
+ if ( typeof arr [ i ] === "number" ) {
113
+ total += arr [ i ] ;
114
+ } else if ( typeof arr [ i ] === "boolean" ) {
115
+ total += arr [ i ] ;
116
+ } else if ( typeof arr [ i ] === "string" ) {
117
+ total += arr [ i ] . length ;
118
+ }
119
+ }
120
+ let average = ( total / arr . length ) . toFixed ( 2 ) ;
121
+
122
+ // Return the value as a num and not a str
123
+ return parseFloat ( average ) ;
124
+ }
125
+
126
+
87
127
// Iteration #5: Unique arrays
88
128
const wordsUnique = [
89
129
'crab' ,
@@ -99,9 +139,39 @@ const wordsUnique = [
99
139
'bring'
100
140
] ;
101
141
142
+ function uniquifyArray ( arr ) {
143
+
144
+ if ( arr . length === 0 ) {
145
+ return null ;
146
+ }
147
+
148
+ let newArr = [ ] ;
149
+
150
+ for ( let i = 0 ; i < arr . length ; i ++ ) {
151
+ if ( newArr . indexOf ( arr [ i ] ) === - 1 ) {
152
+ newArr . push ( arr [ i ] ) ;
153
+ }
154
+ }
155
+ return newArr ;
156
+ }
157
+
102
158
// Iteration #6: Find elements
103
159
const wordsFind = [ 'machine' , 'subset' , 'trouble' , 'starting' , 'matter' , 'eating' , 'truth' , 'disobedience' ] ;
104
160
161
+ function doesWordExist ( arr , word ) {
162
+
163
+ if ( arr . length === 0 ) {
164
+ return null ;
165
+ }
166
+
167
+ for ( let i = 0 ; i < arr . length ; i ++ ) {
168
+ if ( arr [ i ] === word ) {
169
+ return true ;
170
+ }
171
+ }
172
+ return false ;
173
+ }
174
+
105
175
// Iteration #7: Count repetition
106
176
const wordsCount = [
107
177
'machine' ,
@@ -117,6 +187,22 @@ const wordsCount = [
117
187
'matter'
118
188
] ;
119
189
190
+ function howManyTimes ( arr , word ) {
191
+
192
+ if ( arr . length === 0 ) {
193
+ return 0 ;
194
+ }
195
+
196
+ let counter = 0 ;
197
+
198
+ for ( let i = 0 ; i < arr . length ; i ++ ) {
199
+ if ( arr [ i ] === word ) {
200
+ counter += 1 ;
201
+ }
202
+ }
203
+ return counter ;
204
+ }
205
+
120
206
// Iteration #8: Bonus
121
207
122
208
const matrix = [
0 commit comments