1
1
// Iteration #1: Find the maximum
2
+ const maxOfTwoNumbers = ( first_number , another_number ) => {
3
+ return Math . max ( first_number , another_number ) ;
4
+ } ;
2
5
3
6
// Iteration #2: Find longest word
4
7
const words = [ 'mystery' , 'brother' , 'aviator' , 'crocodile' , 'pearl' , 'orchard' , 'crackpot' ] ;
5
8
9
+ const findLongestWord = ( words ) => {
10
+ let longestWordSoFar = '' ;
11
+
12
+ if ( ! words || words . length === 0 ) return null ;
13
+ for ( let word of words ) {
14
+ if ( word . length > longestWordSoFar . length ) longestWordSoFar = word ;
15
+ }
16
+
17
+ return longestWordSoFar ;
18
+ } ;
19
+
6
20
// Iteration #3: Calculate the sum
7
21
8
22
const numbers = [ 6 , 12 , 1 , 18 , 13 , 16 , 2 , 1 , 8 , 10 ] ;
9
23
24
+ const sumNumbers = ( numbers ) => {
25
+ let outcome = 0 ;
26
+
27
+ if ( ! numbers || numbers . length === 0 ) return 0 ;
28
+
29
+ for ( let number of numbers ) {
30
+ if ( typeof number === 'number' ) outcome += number ;
31
+ }
32
+
33
+ return outcome ;
34
+ } ;
35
+
36
+ const mixedArr = [ 6 , 12 , 'miami' , 1 , true , 'barca' , '200' , 'lisboa' , 8 , 10 ] ;
37
+
38
+ const sum = ( mixedArray ) => {
39
+ let output = 0 ;
40
+
41
+ if ( ! mixedArray || mixedArray . length === 0 ) return 0 ;
42
+
43
+ for ( let item of mixedArray ) {
44
+ switch ( typeof item ) {
45
+ case 'number' :
46
+ output += item ;
47
+ break ;
48
+ case 'string' :
49
+ output += item . length ;
50
+ break ;
51
+ case 'boolean' :
52
+ output += item ? 1 : 0 ;
53
+ break ;
54
+ default :
55
+ throw new Error ( "Unsupported data type sir or ma'am" ) ;
56
+ }
57
+ }
58
+
59
+ return output ;
60
+ } ;
61
+
10
62
// Iteration #4: Calculate the average
11
63
// Level 1: Array of numbers
12
64
const numbersAvg = [ 2 , 6 , 9 , 10 , 7 , 4 , 1 , 9 ] ;
13
65
66
+ const averageNumbers = ( numbers ) => {
67
+ let output = 0 ;
68
+
69
+ if ( ! numbers || numbers . length === 0 ) return null ;
70
+
71
+ return sumNumbers ( numbers ) / numbers . length ;
72
+ } ;
73
+
74
+
14
75
// Level 2: Array of strings
15
76
const wordsArr = [ 'seat' , 'correspond' , 'linen' , 'motif' , 'hole' , 'smell' , 'smart' , 'chaos' , 'fuel' , 'palace' ] ;
16
77
78
+ const averageWordLength = ( words ) => {
79
+ // Assuming all items in the array are strings !!!
80
+
81
+ if ( ! words || words . length === 0 ) return null ;
82
+
83
+ const words_length_sum = sum ( words ) ;
84
+ const words_length_average = words_length_sum / words . length ;
85
+ return words_length_average ;
86
+ } ;
87
+
88
+ const avg = ( items ) => {
89
+
90
+ if ( ! items || items . length === 0 ) return null ;
91
+
92
+ const items_length_sum = sum ( items ) ;
93
+
94
+ let items_count = 0 ;
95
+ for ( let item of items ) {
96
+ if ( typeof item === 'number' || typeof item === 'string' || typeof item === 'boolean' ) { items_count ++ ; }
97
+ }
98
+ const items_length_average = items_length_sum / items_count ;
99
+ return items_length_average ;
100
+ } ;
101
+
17
102
// Iteration #5: Unique arrays
18
103
const wordsUnique = [
19
104
'crab' ,
@@ -29,9 +114,35 @@ const wordsUnique = [
29
114
'bring'
30
115
] ;
31
116
117
+ const uniquifyArray = ( words ) => {
118
+
119
+ if ( ! words || words . length === 0 ) return null ;
120
+
121
+ let unique_words = [ ] ;
122
+ for ( let idx in words ) {
123
+ if ( words . indexOf ( words [ idx ] ) >= idx ) {
124
+ unique_words . push ( words [ idx ] ) ;
125
+ }
126
+ }
127
+
128
+ return unique_words ;
129
+ } ;
130
+
32
131
// Iteration #6: Find elements
33
132
const wordsFind = [ 'machine' , 'subset' , 'trouble' , 'starting' , 'matter' , 'eating' , 'truth' , 'disobedience' ] ;
34
133
134
+ const doesWordExist = ( words , search_word ) => {
135
+ if ( ! words || words . length === 0 || ! search_word ) return null ;
136
+
137
+ for ( let word of words ) {
138
+ if ( word === search_word ) {
139
+ return true ;
140
+ }
141
+ }
142
+
143
+ return false ;
144
+ } ;
145
+
35
146
// Iteration #7: Count repetition
36
147
const wordsCount = [
37
148
'machine' ,
@@ -47,6 +158,14 @@ const wordsCount = [
47
158
'matter'
48
159
] ;
49
160
161
+ const howManyTimes = ( words , search_word ) => {
162
+ let counter = 0 ;
163
+ for ( let word of words ) {
164
+ if ( word === search_word ) counter ++ ;
165
+ }
166
+ return counter ;
167
+ } ;
168
+
50
169
// Iteration #8: Bonus
51
170
52
171
const matrix = [
0 commit comments