11// Iteration #1: Find the maximum
2- function maxOfTwoNumbers ( ) { }
3-
4-
5-
2+ function maxOfTwoNumbers ( num1 , num2 ) {
3+ if ( num1 > num2 ) {
4+ return num1 ;
5+ } else {
6+ return num2 ;
7+ }
8+ }
9+ maxOfTwoNumbers ( 3 , 2 ) ;
610// Iteration #2: Find longest word
711const words = [ 'mystery' , 'brother' , 'aviator' , 'crocodile' , 'pearl' , 'orchard' , 'crackpot' ] ;
8-
9- function findLongestWord ( ) { }
12+ const arr = [ ]
13+ function findLongestWord ( banana ) {
14+ // declare a variable store the longestWord
15+ let longestWord = "" ;
16+ // loop over the array
17+ for ( let i = 0 ; i < banana . length ; i ++ ) {
18+ if ( banana [ i ] . length > longestWord . length ) {
19+ longestWord = banana [ i ]
20+ }
21+ return longestWord
22+ }
23+ }
24+ findLongestWord ( words )
25+ findLongestWord ( arr )
1026
1127
1228
1329// Iteration #3: Calculate the sum
1430const numbers = [ 6 , 12 , 1 , 18 , 13 , 16 , 2 , 1 , 8 , 10 ] ;
1531
16- function sumNumbers ( ) { }
17-
32+ function sumNumbers ( arrayOfNumbers ) {
33+ let result = 0 ;
34+ for ( i = 0 ; i < arrayOfNumbers . length ; i ++ ) {
35+ result += arrayOfNumbers [ i ] ;
36+ }
37+ return result ;
38+ }
1839
40+ console . log ( sumNumbers ( numbers ) ) ;
1941
2042// Iteration #3.1 Bonus:
2143function sum ( ) { }
@@ -24,15 +46,28 @@ function sum() {}
2446
2547// Iteration #4: Calculate the average
2648// Level 1: Array of numbers
27- const numbersAvg = [ 2 , 6 , 9 , 10 , 7 , 4 , 1 , 9 ] ;
49+ const numbersAvg = [ 2 , 6 , 9 , 10 , 7 , 4 , 1 , 9 ] ; //6
2850
29- function averageNumbers ( ) { }
51+ function averageNumbers ( array ) {
52+ let average = sumNumbers ( array ) / array . length ;
53+ return average ;
54+ }
3055
56+ console . log ( averageNumbers ( numbersAvg ) ) ;
3157
3258// Level 2: Array of strings
33- const wordsArr = [ 'seat' , 'correspond' , 'linen' , 'motif' , 'hole' , 'smell' , 'smart' , 'chaos' , 'fuel' , 'palace' ] ;
59+ const wordsArr = [ 'seat' , 'correspond' , 'linen' , 'motif' , 'hole' , 'smell' , 'smart' , 'chaos' , 'fuel' , 'palace' ] ; //5,3
60+
61+ function averageWordLength ( arrOfStr ) {
62+ let sumOfChar = 0 ;
63+ for ( j = 0 ; j < arrOfStr . length ; j ++ ) {
64+ sumOfChar += arrOfStr [ j ] . length ;
65+ }
66+ let averageOfChar = sumOfChar / arrOfStr . length ;
67+ return averageOfChar ;
68+ }
3469
35- function averageWordLength ( ) { }
70+ console . log ( averageWordLength ( wordsArr ) ) ;
3671
3772// Bonus - Iteration #4.1
3873function avg ( ) { }
0 commit comments