11// Iteration #1: Find the maximum
2- function maxOfTwoNumbers ( ) { }
2+ function maxOfTwoNumbers ( num1 , num2 ) {
3+ return Math . max ( num1 , num2 ) ;
4+ }
35
6+ console . log ( maxOfTwoNumbers ( 3 , 6 ) ) ;
47
58
69// Iteration #2: Find longest word
710const words = [ 'mystery' , 'brother' , 'aviator' , 'crocodile' , 'pearl' , 'orchard' , 'crackpot' ] ;
811
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 ) ) ;
1022
1123
1224
1325// Iteration #3: Calculate the sum
1426const numbers = [ 6 , 12 , 1 , 18 , 13 , 16 , 2 , 1 , 8 , 10 ] ;
1527
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 ) ) ;
1738
1839
1940
2041// 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 ) ) ;
2263
2364
2465
2566// Iteration #4: Calculate the average
2667// Level 1: Array of numbers
2768const numbersAvg = [ 2 , 6 , 9 , 10 , 7 , 4 , 1 , 9 ] ;
2869
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 ) ) ;
3077
3178
3279// Level 2: Array of strings
3380const wordsArr = [ 'seat' , 'correspond' , 'linen' , 'motif' , 'hole' , 'smell' , 'smart' , 'chaos' , 'fuel' , 'palace' ] ;
3481
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+
3696
3797// Bonus - Iteration #4.1
3898function avg ( ) { }
@@ -52,15 +112,37 @@ const wordsUnique = [
52112 'bring'
53113] ;
54114
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 ) ) ;
56128
57129
58130
59131// Iteration #6: Find elements
60132const wordsFind = [ 'machine' , 'subset' , 'trouble' , 'starting' , 'matter' , 'eating' , 'truth' , 'disobedience' ] ;
61133
62- function doesWordExist ( ) { }
134+ function doesWordExist ( wordsFind , wordToFind ) {
63135
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' ) ;
64146
65147
66148// Iteration #7: Count repetition
0 commit comments