1
1
// 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 ) ;
6
10
// Iteration #2: Find longest word
7
11
const 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 )
10
26
11
27
12
28
13
29
// Iteration #3: Calculate the sum
14
30
const numbers = [ 6 , 12 , 1 , 18 , 13 , 16 , 2 , 1 , 8 , 10 ] ;
15
31
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
+ }
18
39
40
+ console . log ( sumNumbers ( numbers ) ) ;
19
41
20
42
// Iteration #3.1 Bonus:
21
43
function sum ( ) { }
@@ -24,15 +46,28 @@ function sum() {}
24
46
25
47
// Iteration #4: Calculate the average
26
48
// 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
28
50
29
- function averageNumbers ( ) { }
51
+ function averageNumbers ( array ) {
52
+ let average = sumNumbers ( array ) / array . length ;
53
+ return average ;
54
+ }
30
55
56
+ console . log ( averageNumbers ( numbersAvg ) ) ;
31
57
32
58
// 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
+ }
34
69
35
- function averageWordLength ( ) { }
70
+ console . log ( averageWordLength ( wordsArr ) ) ;
36
71
37
72
// Bonus - Iteration #4.1
38
73
function avg ( ) { }
0 commit comments