1
1
// Iteration #1: Find the maximum
2
- function maxOfTwoNumbers ( ) { }
3
-
4
-
2
+ function maxOfTwoNumbers ( num1 , num2 ) {
3
+ if ( num1 > num2 ) {
4
+ return num1 ;
5
+ } else if ( num1 < num2 ) {
6
+ return num2 ;
7
+ } else if ( num1 === num2 ) {
8
+ return num1 , num2 ;
9
+ }
10
+ }
5
11
6
12
// Iteration #2: Find longest word
7
13
const words = [ 'mystery' , 'brother' , 'aviator' , 'crocodile' , 'pearl' , 'orchard' , 'crackpot' ] ;
8
14
9
- function findLongestWord ( ) { }
10
-
11
-
15
+ function findLongestWord ( arrOfWords ) {
16
+ if ( arrOfWords . length === 0 ) {
17
+ return null ;
18
+ }
19
+
20
+ let wordLngth = 0 ;
21
+ let longestWord ;
22
+
23
+ for ( let i = 0 ; i < arrOfWords . length ; i ++ ) {
24
+ if ( arrOfWords [ i ] . length > wordLngth ) {
25
+ wordLngth = arrOfWords [ i ] . length ;
26
+ longestWord = arrOfWords [ i ] ;
27
+ }
28
+ }
29
+ return longestWord ;
30
+ }
12
31
13
32
// Iteration #3: Calculate the sum
14
33
const numbers = [ 6 , 12 , 1 , 18 , 13 , 16 , 2 , 1 , 8 , 10 ] ;
15
34
16
- function sumNumbers ( ) { }
17
-
35
+ function sumNumbers ( arrNumbers ) {
36
+ let sum = 0 ;
18
37
38
+ for ( let i = 0 ; i < arrNumbers . length ; i ++ ) {
39
+ sum += arrNumbers [ i ] ;
40
+ }
41
+ return sum ;
42
+ }
19
43
20
44
// Iteration #3.1 Bonus:
21
- function sum ( ) { }
22
-
23
-
45
+ function sum ( arrOfNumbers ) {
46
+ let sum = 0 ;
47
+ let sumOfString = '' ;
48
+ for ( let i = 0 ; i < arrOfNumbers . length ; i ++ ) {
49
+ sum += arrOfNumbers [ i ] ;
50
+ }
51
+ return sum ;
52
+ }
24
53
25
54
// Iteration #4: Calculate the average
26
55
// Level 1: Array of numbers
27
56
const numbersAvg = [ 2 , 6 , 9 , 10 , 7 , 4 , 1 , 9 ] ;
28
57
29
- function averageNumbers ( ) { }
58
+ function averageNumbers ( arrNumbersAvg ) {
59
+ let sumAvg = 0 ;
60
+ let average = 0 ;
61
+
62
+ if ( arrNumbersAvg . length === 0 ) {
63
+ return null ;
64
+ }
65
+ for ( let i = 0 ; i < arrNumbersAvg . length ; i ++ ) {
66
+ sumAvg += arrNumbersAvg [ i ] ;
67
+ }
68
+ average = sumAvg / arrNumbersAvg . length ;
30
69
70
+ return average ;
71
+ }
31
72
32
73
// Level 2: Array of strings
33
74
const wordsArr = [ 'seat' , 'correspond' , 'linen' , 'motif' , 'hole' , 'smell' , 'smart' , 'chaos' , 'fuel' , 'palace' ] ;
34
75
35
- function averageWordLength ( ) { }
76
+ function averageWordLength ( arr ) {
77
+ if ( arr . length === 0 ) {
78
+ return null ;
79
+ }
80
+ let wordLngthSum = 0 ;
81
+ let wordLngthAvg = 0 ;
82
+
83
+ for ( let i = 0 ; i < arr . length ; i ++ ) {
84
+ wordLngthSum += arr [ i ] . length ;
85
+ }
86
+ wordLngthAvg = wordLngthSum / arr . length ;
87
+
88
+ return wordLngthAvg ;
89
+ }
36
90
37
91
// Bonus - Iteration #4.1
38
92
function avg ( ) { }
@@ -52,16 +106,40 @@ const wordsUnique = [
52
106
'bring'
53
107
] ;
54
108
55
- function uniquifyArray ( ) { }
109
+ function uniquifyArray ( arr ) {
110
+ let doubles = [ ] ;
56
111
112
+ if ( arr . length === 0 ) {
113
+ return null ;
114
+ }
57
115
116
+ for ( let i = 0 ; i < arr . length ; i ++ ) {
117
+ if ( doubles . indexOf ( arr [ i ] ) === - 1 ) {
118
+ doubles . push ( arr [ i ] ) ;
119
+ }
120
+ }
121
+ return doubles ;
122
+ }
58
123
59
124
// Iteration #6: Find elements
60
125
const wordsFind = [ 'machine' , 'subset' , 'trouble' , 'starting' , 'matter' , 'eating' , 'truth' , 'disobedience' ] ;
61
126
62
- function doesWordExist ( ) { }
63
-
64
-
127
+ function doesWordExist ( arrOfWords , searchWord ) {
128
+ let rest = [ ] ;
129
+
130
+ if ( arrOfWords . length === 0 ) {
131
+ return null ;
132
+ }
133
+ for ( let i = 0 ; i < arrOfWords . length ; i ++ ) {
134
+ if ( searchWord === arrOfWords [ i ] ) {
135
+ rest . push ( arrOfWords [ i ] ) ;
136
+ return true ;
137
+ }
138
+ }
139
+ if ( rest . length === 0 ) {
140
+ return false ;
141
+ }
142
+ }
65
143
66
144
// Iteration #7: Count repetition
67
145
const wordsCount = [
@@ -78,9 +156,27 @@ const wordsCount = [
78
156
'matter'
79
157
] ;
80
158
81
- function howManyTimes ( ) { }
82
-
83
-
159
+ function howManyTimes ( arrOfWords , searchWord ) {
160
+ let rest = [ ] ;
161
+
162
+ if ( arrOfWords . length === 0 ) {
163
+ return 0 ;
164
+ }
165
+
166
+ if ( arrOfWords . length === 0 ) {
167
+ return null ;
168
+ }
169
+ for ( let i = 0 ; i < arrOfWords . length ; i ++ ) {
170
+ if ( searchWord === arrOfWords [ i ] ) {
171
+ rest . push ( arrOfWords [ i ] ) ;
172
+ }
173
+ }
174
+ return rest . length ;
175
+
176
+ if ( rest . length === 0 ) {
177
+ return "This word doesn't exist!" ;
178
+ }
179
+ }
84
180
85
181
// Iteration #8: Bonus
86
182
const matrix = [
@@ -108,9 +204,6 @@ const matrix = [
108
204
109
205
function greatestProduct ( ) { }
110
206
111
-
112
-
113
-
114
207
// The following is required to make unit tests work.
115
208
/* Environment setup. Do not modify the below code. */
116
209
if ( typeof module !== 'undefined' ) {
0 commit comments