1
1
// Iteration #1: Find the maximum
2
- function maxOfTwoNumbers ( ) { }
3
-
4
-
2
+ function maxOfTwoNumbers ( num1 , num2 ) {
3
+ // JS's Math.max own function calculates the highest among two numbers
4
+ return Math . max ( num1 , num2 ) ;
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
-
11
-
10
+ function findLongestWord ( arr ) {
11
+ // Check for empty array
12
+ if ( ! arr . length ) return null ;
13
+
14
+ // Initialize variable to contain largest word found
15
+ let largestWord = "" ;
16
+
17
+ // Loop to check every word in array. Largest will be stored in "largestWord"
18
+ arr . forEach ( element => {
19
+ if ( largestWord . length < element . length ) {
20
+ largestWord = element ;
21
+ }
22
+ } ) ;
23
+ return largestWord ;
24
+ }
12
25
13
26
// Iteration #3: Calculate the sum
14
27
const numbers = [ 6 , 12 , 1 , 18 , 13 , 16 , 2 , 1 , 8 , 10 ] ;
15
28
16
- function sumNumbers ( ) { }
17
-
18
-
29
+ function sumNumbers ( arr ) {
30
+ let summ = 0 ;
31
+ // Loop to add each number on array to summ
32
+ for ( item of arr ) {
33
+ summ += item ;
34
+ }
35
+ return summ ;
36
+ }
19
37
20
38
// Iteration #3.1 Bonus:
21
- function sum ( ) { }
22
-
23
39
40
+ function sum ( arr ) {
41
+ let summ = 0 ;
42
+ // Loop goes through each number in array
43
+ for ( item of arr ) {
44
+ // Objects throw error message
45
+ if ( typeof item === 'object' ) {
46
+ throw new Error ( "Unsupported data type sir or ma'am" ) ;
47
+ }
48
+ // Strings have their length added to summ
49
+ else if ( typeof item === 'string' )
50
+ {
51
+ item = item . length ;
52
+ }
53
+ // Numbers are just added
54
+ else {
55
+ item = Number ( item ) ;
56
+ }
57
+ summ += item ;
58
+ }
59
+ return summ ;
60
+ }
24
61
25
62
// Iteration #4: Calculate the average
26
63
// Level 1: Array of numbers
27
64
const numbersAvg = [ 2 , 6 , 9 , 10 , 7 , 4 , 1 , 9 ] ;
28
65
29
- function averageNumbers ( ) { }
66
+ function averageNumbers ( arr ) {
67
+ // Check for empty array
68
+ if ( arr . length === 0 ) {
69
+ return null ;
70
+ }
71
+
72
+ // Invoke "sum" to have the summage of all elements
73
+ summ = sum ( arr ) ;
74
+
75
+ // Average is the division of sum by the number of array's elements
76
+ const avg = summ / arr . length ;
77
+ return avg ;
78
+ }
30
79
31
80
32
81
// Level 2: Array of strings
33
82
const wordsArr = [ 'seat' , 'correspond' , 'linen' , 'motif' , 'hole' , 'smell' , 'smart' , 'chaos' , 'fuel' , 'palace' ] ;
34
83
35
- function averageWordLength ( ) { }
84
+ function averageWordLength ( arr ) {
85
+ // Check for empty array
86
+ if ( ! arr . length ) return null ;
87
+
88
+ // Invoke function 'sum' which already adds values of string lenghts
89
+ let summ = sum ( arr ) ;
90
+
91
+ // Average is the division of sum by the number of array's elements
92
+ const avg = summ / arr . length ;
93
+ return avg ;
94
+ }
36
95
37
96
// Bonus - Iteration #4.1
38
- function avg ( ) { }
97
+ function avg ( arr ) {
98
+ // Check for empty array
99
+ if ( arr . length === 0 ) {
100
+ return null ;
101
+ }
102
+
103
+ // Invoke function 'sum' which already adds data types requested
104
+ summ = sum ( arr )
105
+
106
+ // Average is the division of sum by the number of array's elements, this time fixed to 2 decimal points
107
+ const avg = Number ( ( summ / arr . length ) . toFixed ( 2 ) ) ;
108
+ return avg ;
109
+ }
39
110
40
111
// Iteration #5: Unique arrays
41
112
const wordsUnique = [
@@ -52,14 +123,32 @@ const wordsUnique = [
52
123
'bring'
53
124
] ;
54
125
55
- function uniquifyArray ( ) { }
56
-
57
-
126
+ function uniquifyArray ( arr ) {
127
+ // Check for empty array
128
+ if ( ! arr . length ) return null ;
129
+
130
+ let newArr = [ ] ;
131
+
132
+ for ( let i = 0 ; i < arr . length ; i ++ ) {
133
+ if ( i === arr . indexOf ( arr [ i ] ) ) {
134
+ newArr . push ( arr [ i ] ) ;
135
+ }
136
+ }
137
+ return newArr ;
138
+ }
58
139
59
140
// Iteration #6: Find elements
60
141
const wordsFind = [ 'machine' , 'subset' , 'trouble' , 'starting' , 'matter' , 'eating' , 'truth' , 'disobedience' ] ;
61
-
62
- function doesWordExist ( ) { }
142
+ word = 'subset' ;
143
+
144
+ function doesWordExist ( arr , word ) {
145
+ for ( let i = 0 ; i < arr . length ; i ++ ) {
146
+ if ( word === arr [ i ] ) {
147
+ return true ;
148
+ break ;
149
+ }
150
+ }
151
+ }
63
152
64
153
65
154
0 commit comments