1
1
// Iteration #1: Find the maximum
2
2
function maxOfTwoNumbers ( num1 , num2 ) {
3
- const isGreater = num1 > num2
3
+ const isGreater = num1 > num2 ;
4
4
if ( isGreater ) {
5
- return num1
5
+ return num1 ;
6
6
} else {
7
- return num2
7
+ return num2 ;
8
8
}
9
9
}
10
10
11
-
12
-
13
11
// Iteration #2: Find longest word
14
12
const words = [ 'mystery' , 'brother' , 'aviator' , 'crocodile' , 'pearl' , 'orchard' , 'crackpot' ] ;
15
13
@@ -18,9 +16,9 @@ function findLongestWord(words) {
18
16
if ( isEmptyArr ) {
19
17
return null ;
20
18
} else {
21
- let longestWord = ""
19
+ let longestWord = '' ;
22
20
for ( const word in words ) {
23
- const isLonger = words [ word ] . length > longestWord . length
21
+ const isLonger = words [ word ] . length > longestWord . length ;
24
22
if ( isLonger ) {
25
23
longestWord = words [ word ] ;
26
24
}
@@ -29,8 +27,6 @@ function findLongestWord(words) {
29
27
}
30
28
}
31
29
32
-
33
-
34
30
// Iteration #3: Calculate the sum
35
31
const numbers = [ 6 , 12 , 1 , 18 , 13 , 16 , 2 , 1 , 8 , 10 ] ;
36
32
@@ -42,22 +38,21 @@ function sumNumbers(numbers) {
42
38
return sum ;
43
39
}
44
40
45
-
46
41
const mixedArr = [ 6 , 12 , 'miami' , 1 , true , 'barca' , '200' , 'lisboa' , 8 , 10 ] ;
47
42
// Iteration #3.1 Bonus:
48
43
function sum ( mixedArr ) {
49
44
let sum = 0 ;
50
45
for ( let num of mixedArr ) {
51
- const isString = typeof num === 'string'
52
- const isNotSupported = typeof num === 'object'
46
+ const isString = typeof num === 'string' ;
47
+ const isNotSupported = typeof num === 'object' ;
53
48
if ( isNotSupported ) {
54
- throw Error ( "Unsupported data type sir or ma'am" )
49
+ throw Error ( "Unsupported data type sir or ma'am" ) ;
55
50
}
56
51
if ( isString ) {
57
52
sum += num . length ;
58
53
continue ;
59
54
}
60
- sum += num
55
+ sum += num ;
61
56
}
62
57
return sum ;
63
58
}
@@ -69,23 +64,28 @@ const numbersAvg = [2, 6, 9, 10, 7, 4, 1, 9];
69
64
function averageNumbers ( numbersAvg ) {
70
65
const isEmptyArr = numbersAvg . length === 0 ;
71
66
if ( isEmptyArr ) {
72
- return null
67
+ return null ;
73
68
} else {
74
- let sum = sumNumbers ( numbersAvg )
75
- let avg = sum / numbersAvg . length
69
+ let sum = sumNumbers ( numbersAvg ) ;
70
+ let avg = sum / numbersAvg . length ;
76
71
return avg ;
77
72
}
78
-
79
73
}
80
74
81
-
82
75
// Level 2: Array of strings
83
76
const wordsArr = [ 'seat' , 'correspond' , 'linen' , 'motif' , 'hole' , 'smell' , 'smart' , 'chaos' , 'fuel' , 'palace' ] ;
84
77
85
- function averageWordLength ( ) { }
86
-
78
+ function averageWordLength ( wordsArr ) {
79
+ const isEmptyArr = wordsArr . length === 0 ;
80
+ if ( isEmptyArr ) {
81
+ return null ;
82
+ } else {
83
+ const result = sum ( wordsArr ) ;
84
+ return result / wordsArr . length ;
85
+ }
86
+ }
87
87
// Bonus - Iteration #4.1
88
- function avg ( ) { }
88
+ function avg ( ) { }
89
89
90
90
// Iteration #5: Unique arrays
91
91
const wordsUnique = [
@@ -102,16 +102,12 @@ const wordsUnique = [
102
102
'bring'
103
103
] ;
104
104
105
- function uniquifyArray ( ) { }
106
-
107
-
105
+ function uniquifyArray ( ) { }
108
106
109
107
// Iteration #6: Find elements
110
108
const wordsFind = [ 'machine' , 'subset' , 'trouble' , 'starting' , 'matter' , 'eating' , 'truth' , 'disobedience' ] ;
111
109
112
- function doesWordExist ( ) { }
113
-
114
-
110
+ function doesWordExist ( ) { }
115
111
116
112
// Iteration #7: Count repetition
117
113
const wordsCount = [
@@ -128,9 +124,7 @@ const wordsCount = [
128
124
'matter'
129
125
] ;
130
126
131
- function howManyTimes ( ) { }
132
-
133
-
127
+ function howManyTimes ( ) { }
134
128
135
129
// Iteration #8: Bonus
136
130
const matrix = [
@@ -156,10 +150,7 @@ const matrix = [
156
150
[ 1 , 70 , 54 , 71 , 83 , 51 , 54 , 69 , 16 , 92 , 33 , 48 , 61 , 43 , 52 , 1 , 89 , 19 , 67 , 48 ]
157
151
] ;
158
152
159
- function greatestProduct ( ) { }
160
-
161
-
162
-
153
+ function greatestProduct ( ) { }
163
154
164
155
// The following is required to make unit tests work.
165
156
/* Environment setup. Do not modify the below code. */
0 commit comments