1
1
// Iteration #1: Find the maximum
2
- function maxOfTwoNumbers ( ) { }
3
-
2
+ function maxOfTwoNumbers ( a , b ) {
3
+ return Math . max ( a , b ) ;
4
+ }
4
5
5
6
6
7
// Iteration #2: Find longest word
7
8
const words = [ 'mystery' , 'brother' , 'aviator' , 'crocodile' , 'pearl' , 'orchard' , 'crackpot' ] ;
8
9
9
- function findLongestWord ( ) { }
10
-
10
+ function findLongestWord ( words ) {
11
+ if ( ! words . length ) {
12
+ return null ;
13
+ } else if ( words . length === 1 ) {
14
+ return words [ 0 ] ;
15
+ } else {
16
+ return words . sort ( ( a , b ) => b . length - a . length ) [ 0 ] ;
17
+ }
18
+ }
11
19
12
20
13
21
// Iteration #3: Calculate the sum
14
22
const numbers = [ 6 , 12 , 1 , 18 , 13 , 16 , 2 , 1 , 8 , 10 ] ;
15
23
16
- function sumNumbers ( ) { }
17
-
24
+ function sumNumbers ( numbers ) {
25
+ let sum = 0 ;
26
+ for ( let number of numbers ) {
27
+ sum += number ;
28
+ }
29
+ return sum ;
30
+ }
18
31
19
32
20
33
// Iteration #3.1 Bonus:
21
- function sum ( ) { }
22
-
34
+ function sum ( dataArray ) {
35
+ let sum = 0 ;
36
+ for ( let element of dataArray ) {
37
+ switch ( typeof element ) {
38
+ case "number" :
39
+ case "boolean" :
40
+ sum += element ;
41
+ break ;
42
+ case "string" :
43
+ sum += element . length ;
44
+ break ;
45
+ default :
46
+ throw new Error ( "Unsupported data type sir or ma'am" ) ;
47
+ break ;
48
+ }
49
+ }
50
+ return sum ;
51
+ }
23
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 ( ) { }
30
-
58
+ function averageNumbers ( numbers ) {
59
+ if ( numbers . length ) {
60
+ return sumNumbers ( numbers ) / numbers . length ;
61
+ } else {
62
+ return null ;
63
+ }
64
+ }
31
65
32
66
// Level 2: Array of strings
33
67
const wordsArr = [ 'seat' , 'correspond' , 'linen' , 'motif' , 'hole' , 'smell' , 'smart' , 'chaos' , 'fuel' , 'palace' ] ;
34
68
35
- function averageWordLength ( ) { }
69
+ function averageWordLength ( stringArray ) {
70
+ if ( stringArray . length ) {
71
+ sum = 0 ;
72
+ for ( let string of stringArray ) {
73
+ sum += string . length ;
74
+ }
75
+ return parseInt ( sum / stringArray . length ) ;
76
+ } else {
77
+ return null ;
78
+ }
79
+ }
36
80
37
81
// Bonus - Iteration #4.1
38
- function avg ( ) { }
82
+ function avg ( dataArray ) {
83
+ if ( dataArray . length ) {
84
+ let sum = 0 ;
85
+ for ( let element of dataArray ) {
86
+ switch ( typeof element ) {
87
+ case "number" :
88
+ case "boolean" :
89
+ sum += element ;
90
+ break ;
91
+ case "string" :
92
+ sum += element . length ;
93
+ break ;
94
+ default :
95
+ throw new Error ( "Unsupported data type sir or ma'am" ) ;
96
+ break ;
97
+ }
98
+ }
99
+ return sum / dataArray . length ;
100
+ } else {
101
+ return null ;
102
+ }
103
+ }
39
104
40
105
// Iteration #5: Unique arrays
41
106
const wordsUnique = [
@@ -52,15 +117,27 @@ const wordsUnique = [
52
117
'bring'
53
118
] ;
54
119
55
- function uniquifyArray ( ) { }
120
+ function uniquifyArray ( array ) {
121
+ if ( array . length ) {
122
+ let cleanedArray = [ ] ;
123
+ for ( let element of array ) {
124
+ if ( ! cleanedArray . includes ( element ) ) {
125
+ cleanedArray . push ( element ) ;
126
+ }
127
+ } return cleanedArray ;
128
+ } else {
129
+ return null ;
130
+ }
131
+ }
56
132
57
133
58
134
59
135
// Iteration #6: Find elements
60
136
const wordsFind = [ 'machine' , 'subset' , 'trouble' , 'starting' , 'matter' , 'eating' , 'truth' , 'disobedience' ] ;
61
137
62
- function doesWordExist ( ) { }
63
-
138
+ function doesWordExist ( wordList , searchWord ) {
139
+ return ( wordList . length ) ? wordList . includes ( searchWord ) : null ;
140
+ }
64
141
65
142
66
143
// Iteration #7: Count repetition
@@ -78,8 +155,22 @@ const wordsCount = [
78
155
'matter'
79
156
] ;
80
157
81
- function howManyTimes ( ) { }
82
-
158
+ function howManyTimes ( wordList , searchWord ) {
159
+ if ( wordList . length ) {
160
+ let occurances = 0 ;
161
+ for ( let word of wordList ) {
162
+ if ( word === searchWord ) {
163
+ console . log ( word ) ;
164
+ occurances ++ ;
165
+ } else {
166
+ continue ;
167
+ }
168
+ }
169
+ return occurances ;
170
+ } else {
171
+ return 0 ;
172
+ }
173
+ }
83
174
84
175
85
176
// Iteration #8: Bonus
0 commit comments