1
1
// Iteration #1: Find the maximum
2
- function maxOfTwoNumbers ( ) { }
2
+ function maxOfTwoNumbers ( num1 , num2 ) {
3
+ return Math . max ( num1 , num2 ) ;
4
+ }
3
5
6
+ console . log ( maxOfTwoNumbers ( 3 , 6 ) ) ;
4
7
5
8
6
9
// Iteration #2: Find longest word
7
10
const words = [ 'mystery' , 'brother' , 'aviator' , 'crocodile' , 'pearl' , 'orchard' , 'crackpot' ] ;
8
11
9
- function findLongestWord ( ) { }
12
+ function findLongestWord ( words ) {
13
+ let maxWord = [ ] ;
14
+ words . forEach ( function ( long ) {
15
+ maxWord . push ( long . length )
16
+ } )
17
+ let theBiggestNumber = Math . max . apply ( null , maxWord ) ; //9
18
+ let theBiggestNumerPosition = maxWord . indexOf ( theBiggestNumber ) ;
19
+ return `The longest word is: ${ words [ theBiggestNumerPosition ] } ` ;
20
+ }
21
+ console . log ( findLongestWord ( words ) ) ;
10
22
11
23
12
24
13
25
// Iteration #3: Calculate the sum
14
26
const numbers = [ 6 , 12 , 1 , 18 , 13 , 16 , 2 , 1 , 8 , 10 ] ;
15
27
16
- function sumNumbers ( ) { }
28
+ function sumNumbers ( numbers ) {
29
+ let total = 0 ;
30
+ for ( let i in numbers ) {
31
+ total += numbers [ i ] ;
32
+ }
33
+ return total ;
34
+
35
+ }
36
+
37
+ console . log ( sumNumbers ( numbers ) ) ;
17
38
18
39
19
40
20
41
// Iteration #3.1 Bonus:
21
- function sum ( ) { }
42
+ const mixedArr = [ 6 , 12 , 'miami' , 1 , true , 'barca' , '200' , 'lisboa' , 8 , 10 ] ;
43
+ function sum ( mixedArr ) {
44
+ let arrayToNum = [ ] ;
45
+ mixedArr . forEach ( function ( ifString ) {
46
+ if ( typeof ( ifString ) == "string" ) {
47
+ arrayToNum . push ( ifString . length ) ;
48
+ } else if ( typeof ( ifString ) == "boolean" ) {
49
+ let booleanToNumber = ifString ? 1 : 0 ;
50
+ arrayToNum . push ( booleanToNumber ) ;
51
+ } else if ( typeof ( ifString ) == "number" ) {
52
+ arrayToNum . push ( ifString ) ;
53
+ }
54
+ } )
55
+ let totalSum = 0 ;
56
+ for ( let i in arrayToNum ) {
57
+ totalSum += arrayToNum [ i ] ;
58
+ }
59
+ return totalSum ;
60
+ }
61
+
62
+ console . log ( sum ( mixedArr ) ) ;
22
63
23
64
24
65
25
66
// Iteration #4: Calculate the average
26
67
// Level 1: Array of numbers
27
68
const numbersAvg = [ 2 , 6 , 9 , 10 , 7 , 4 , 1 , 9 ] ;
28
69
29
- function averageNumbers ( ) { }
70
+ function averageNumbers ( numbersAvg ) {
71
+
72
+ let averageNum = sumNumbers ( numbersAvg ) / numbersAvg . length ;
73
+ return averageNum ;
74
+ }
75
+
76
+ console . log ( averageNumbers ( numbersAvg ) ) ;
30
77
31
78
32
79
// Level 2: Array of strings
33
80
const wordsArr = [ 'seat' , 'correspond' , 'linen' , 'motif' , 'hole' , 'smell' , 'smart' , 'chaos' , 'fuel' , 'palace' ] ;
34
81
35
- function averageWordLength ( ) { }
82
+ //[4, 10, 5, 5, 4, 5, 5, 5, 4, 6]
83
+
84
+ function averageWordLength ( wordsArr ) {
85
+
86
+ let eachWordLength = [ ] ;
87
+ wordsArr . forEach ( function ( wordLong ) {
88
+ eachWordLength . push ( wordLong . length )
89
+ } )
90
+ return averageNumbers ( eachWordLength ) ;
91
+
92
+ }
93
+
94
+ console . log ( averageWordLength ( wordsArr ) ) ;
95
+
36
96
37
97
// Bonus - Iteration #4.1
38
98
function avg ( ) { }
@@ -52,15 +112,37 @@ const wordsUnique = [
52
112
'bring'
53
113
] ;
54
114
55
- function uniquifyArray ( ) { }
115
+ function uniquifyArray ( wordsUnique ) {
116
+
117
+ let uniqueWords = [ ] ;
118
+
119
+ wordsUnique . forEach ( function ( thing ) {
120
+ if ( ! uniqueWords . includes ( thing ) ) {
121
+ uniqueWords . push ( thing ) ;
122
+ }
123
+ } )
124
+ return uniqueWords ;
125
+ }
126
+
127
+ console . log ( uniquifyArray ( wordsUnique ) ) ;
56
128
57
129
58
130
59
131
// Iteration #6: Find elements
60
132
const wordsFind = [ 'machine' , 'subset' , 'trouble' , 'starting' , 'matter' , 'eating' , 'truth' , 'disobedience' ] ;
61
133
62
- function doesWordExist ( ) { }
134
+ function doesWordExist ( wordsFind , wordToFind ) {
63
135
136
+ wordsFind . forEach ( function ( find ) {
137
+ if ( wordsFind . includes ( wordToFind ) ) {
138
+ console . log ( true ) ;
139
+ } else {
140
+ console . log ( false ) ;
141
+ }
142
+ } )
143
+
144
+ }
145
+ doesWordExist ( wordsFind , 'machine' ) ;
64
146
65
147
66
148
// Iteration #7: Count repetition
0 commit comments