1
1
// Iteration #1: Find the maximum
2
- function maxOfTwoNumbers ( ) { }
3
-
4
-
5
-
2
+ function maxOfTwoNumbers ( a , b ) {
3
+ if ( a > b ) {
4
+ return a ;
5
+ } else if ( b > a ) {
6
+ return b ;
7
+ } else if ( a === b ) {
8
+ return a && b ;
9
+ }
10
+ }
6
11
// Iteration #2: Find longest word
7
12
const words = [ 'mystery' , 'brother' , 'aviator' , 'crocodile' , 'pearl' , 'orchard' , 'crackpot' ] ;
8
13
9
- function findLongestWord ( ) { }
10
-
11
-
14
+ function findLongestWord ( words ) {
15
+ let hugeWord = '' ;
16
+ if ( words . length === 0 ) {
17
+ return null ;
18
+ } else if ( words . length === 1 ) {
19
+ return words [ 0 ] ;
20
+ } else {
21
+ for ( let i = 0 ; i < words . length ; i ++ ) {
22
+ if ( words [ i ] . length > hugeWord . length ) {
23
+ hugeWord = words [ i ] ;
24
+ }
25
+ }
26
+ }
27
+ return hugeWord ;
28
+ }
12
29
13
30
// Iteration #3: Calculate the sum
14
31
const numbers = [ 6 , 12 , 1 , 18 , 13 , 16 , 2 , 1 , 8 , 10 ] ;
15
32
16
- function sumNumbers ( ) { }
17
-
18
-
33
+ function sumNumbers ( numbers ) {
34
+ let totalSum = 0 ;
35
+ for ( let i = 0 ; i < numbers . length ; i ++ ) {
36
+ totalSum += numbers [ i ] ;
37
+ }
38
+ return totalSum ;
39
+ }
19
40
20
41
// Iteration #3.1 Bonus:
21
42
function sum ( ) { }
22
43
23
-
24
-
25
44
// Iteration #4: Calculate the average
26
45
// Level 1: Array of numbers
27
46
const numbersAvg = [ 2 , 6 , 9 , 10 , 7 , 4 , 1 , 9 ] ;
28
47
29
- function averageNumbers ( ) { }
48
+ function averageNumbers ( numbersAvg ) {
49
+ if ( numbersAvg . length === 0 ) {
50
+ return null ;
51
+ }
30
52
53
+ let totalSum = 0 ;
54
+ for ( let i = 0 ; i < numbersAvg . length ; i ++ ) {
55
+ totalSum += numbersAvg [ i ] ;
56
+ }
57
+ return totalSum / numbersAvg . length ;
58
+ }
31
59
32
60
// Level 2: Array of strings
33
61
const wordsArr = [ 'seat' , 'correspond' , 'linen' , 'motif' , 'hole' , 'smell' , 'smart' , 'chaos' , 'fuel' , 'palace' ] ;
34
62
35
- function averageWordLength ( ) { }
63
+ function averageWordLength ( wordsArr ) {
64
+ if ( wordsArr . length === 0 ) {
65
+ return null ;
66
+ }
67
+ let totalSumOfLetters = 0 ;
68
+ for ( let i = 0 ; i < wordsArr . length ; i ++ ) {
69
+ totalSumOfLetters += wordsArr [ i ] . length ;
70
+ }
71
+ return totalSumOfLetters / wordsArr . length ;
72
+ }
36
73
37
74
// Bonus - Iteration #4.1
38
75
function avg ( ) { }
@@ -52,16 +89,27 @@ const wordsUnique = [
52
89
'bring'
53
90
] ;
54
91
55
- function uniquifyArray ( ) { }
56
-
57
-
92
+ function uniquifyArray ( wordsUnique ) {
93
+ if ( wordsUnique . length === 0 ) {
94
+ return null ;
95
+ }
96
+ }
58
97
59
98
// Iteration #6: Find elements
60
99
const wordsFind = [ 'machine' , 'subset' , 'trouble' , 'starting' , 'matter' , 'eating' , 'truth' , 'disobedience' ] ;
61
-
62
- function doesWordExist ( ) { }
63
-
64
-
100
+ let givenArr ;
101
+ function doesWordExist ( wordsFind , givenArr ) {
102
+ for ( let i = 0 ; i < wordsFind . length ; i ++ ) {
103
+ if ( givenArr === wordsFind [ i ] ) {
104
+ return true ;
105
+ }
106
+ }
107
+ if ( wordsFind . length === 0 ) {
108
+ return null ;
109
+ } else {
110
+ return false ;
111
+ }
112
+ }
65
113
66
114
// Iteration #7: Count repetition
67
115
const wordsCount = [
@@ -77,10 +125,17 @@ const wordsCount = [
77
125
'disobedience' ,
78
126
'matter'
79
127
] ;
80
-
81
- function howManyTimes ( ) { }
82
-
83
-
128
+ let oneWordToSearch ;
129
+
130
+ function howManyTimes ( wordsCount , oneWordToSearch ) {
131
+ let counterOfWords = 0 ;
132
+ for ( let i = 0 ; i < wordsCount . length ; i ++ ) {
133
+ if ( oneWordToSearch === wordsCount [ i ] ) {
134
+ counterOfWords ++ ;
135
+ }
136
+ }
137
+ return counterOfWords ;
138
+ }
84
139
85
140
// Iteration #8: Bonus
86
141
const matrix = [
@@ -108,9 +163,6 @@ const matrix = [
108
163
109
164
function greatestProduct ( ) { }
110
165
111
-
112
-
113
-
114
166
// The following is required to make unit tests work.
115
167
/* Environment setup. Do not modify the below code. */
116
168
if ( typeof module !== 'undefined' ) {
0 commit comments