1
1
// Iteration #1: Find the maximum
2
- function maxOfTwoNumbers ( ) { }
3
-
4
-
2
+ function maxOfTwoNumbers ( a , b ) {
3
+ if ( a > b ) {
4
+ return a
5
+ } else if ( a < b ) {
6
+ return b
7
+ } else {
8
+ return a
9
+ }
10
+ }
5
11
6
12
// Iteration #2: Find longest word
7
13
const words = [ 'mystery' , 'brother' , 'aviator' , 'crocodile' , 'pearl' , 'orchard' , 'crackpot' ] ;
8
14
9
- function findLongestWord ( ) { }
10
-
15
+ function findLongestWord ( words ) {
16
+ //Check empty. return null
17
+ let longestWord = "" ;
18
+ if ( words . length === 0 ) {
19
+ return null ;
20
+ } else if ( words . length === 1 ) {
21
+ return words [ 0 ]
22
+ } else {
23
+ for ( i = 0 ; i < words . length ; i ++ ) {
24
+
25
+ if ( longestWord . length < words [ i ] . length ) {
26
+ longestWord = words [ i ]
27
+ }
28
+ }
29
+ return longestWord ;
30
+ }
31
+ //First iteration if same length
32
+ }
33
+ findLongestWord ( words )
11
34
12
35
13
36
// Iteration #3: Calculate the sum
14
37
const numbers = [ 6 , 12 , 1 , 18 , 13 , 16 , 2 , 1 , 8 , 10 ] ;
15
38
16
- function sumNumbers ( ) { }
39
+ function sumNumbers ( numbers ) {
40
+
41
+ if ( numbers . length === 0 ) {
42
+ return 0
43
+ } else if ( numbers . length === 1 ) {
44
+ return numbers [ 0 ]
45
+ } else {
46
+ let sum = 0
47
+ for ( i = 0 ; i < numbers . length ; i ++ ) {
48
+ sum += numbers [ i ]
49
+ }
50
+ return sum
51
+ }
52
+ }
17
53
54
+ // Iteration #3.1 Bonus:
55
+ // const mixedArr = [6, 12, 'miami', 1, true, 'barca', '200', 'lisboa', 8, 10];
18
56
57
+ // // should return: 57
19
58
20
- // Iteration #3.1 Bonus:
21
- function sum ( ) { }
59
+ function sum ( mixedArr ) {
60
+ // if (number.length === 0) {
61
+ // return 0
62
+ // } else if (number.length === 1) {
63
+ // return number[0]
64
+ // } else {
65
+ // let sum = 0
66
+ // for (i = 0; i < number.length; i++){
67
+ // sum += number[i]
68
+ // }
69
+ // return sum
70
+ }
22
71
23
72
24
73
25
74
// Iteration #4: Calculate the average
26
75
// Level 1: Array of numbers
27
76
const numbersAvg = [ 2 , 6 , 9 , 10 , 7 , 4 , 1 , 9 ] ;
28
77
29
- function averageNumbers ( ) { }
78
+ function averageNumbers ( numberArr ) {
79
+ let sum = 0
80
+ let arrLength = numberArr . length
81
+ if ( numberArr . length === 0 ) {
82
+ return null
83
+ } else if ( numberArr . length === 1 ) {
84
+ return numberArr [ 0 ]
85
+ }
86
+ else {
87
+ for ( i = 0 ; i < arrLength ; i ++ ) {
88
+ sum += numberArr [ i ]
89
+ }
90
+ return sum / arrLength
91
+ }
92
+ }
93
+
94
+ //console.log(averageNumbers(numbersAvg))
30
95
31
96
32
97
// Level 2: Array of strings
@@ -52,9 +117,21 @@ const wordsUnique = [
52
117
'bring'
53
118
] ;
54
119
55
- function uniquifyArray ( ) { }
120
+ function uniquifyArray ( wordsUnique ) {
121
+ if ( wordsUnique . length === 0 ) { return null }
56
122
123
+ let collectWords = [ ]
124
+ for ( i = 0 ; i < wordsUnique . length ; i ++ ) {
125
+ //console.log(collectWords.indexOf(wordsUnique[i]));
126
+ if ( collectWords . indexOf ( wordsUnique [ i ] ) === - 1 ) {
127
+ collectWords . push ( wordsUnique [ i ] )
128
+ //console.log(collectWords);
129
+ }
130
+ }
131
+ return collectWords
132
+ }
57
133
134
+ uniquifyArray ( wordsUnique )
58
135
59
136
// Iteration #6: Find elements
60
137
const wordsFind = [ 'machine' , 'subset' , 'trouble' , 'starting' , 'matter' , 'eating' , 'truth' , 'disobedience' ] ;
0 commit comments