1
1
// Iteration #1: Find the maximum
2
2
3
+ function maxOfTwoNumbers ( number1 , number2 ) {
4
+ if ( number1 > number2 ) {
5
+ return number1
6
+ } else {
7
+ return number2
8
+ }
9
+ }
10
+
3
11
// Iteration #2: Find longest word
4
12
const words = [ 'mystery' , 'brother' , 'aviator' , 'crocodile' , 'pearl' , 'orchard' , 'crackpot' ] ;
5
13
14
+ function findLongestWord ( arr ) {
15
+ if ( arr . length == 0 ) {
16
+ return null
17
+ }
18
+ let longest = ""
19
+ for ( let i = 0 ; i <= arr . length - 1 ; i ++ ) {
20
+ if ( longest . length < arr [ i ] . length ) {
21
+ longest = arr [ i ] ;
22
+ }
23
+ }
24
+ return longest
25
+ }
26
+
27
+
6
28
// Iteration #3: Calculate the sum
7
29
8
30
const numbers = [ 6 , 12 , 1 , 18 , 13 , 16 , 2 , 1 , 8 , 10 ] ;
9
31
32
+ function sumNumbers ( arr ) {
33
+ if ( arr == "" ) {
34
+ return 0
35
+ }
36
+ let total = 0
37
+ for ( let i = 0 ; i < arr . length ; i ++ ) {
38
+ total += Number ( arr [ i ] ) ;
39
+ }
40
+ return total
41
+ }
42
+
10
43
// Iteration #4: Calculate the average
44
+
11
45
// Level 1: Array of numbers
12
46
const numbersAvg = [ 2 , 6 , 9 , 10 , 7 , 4 , 1 , 9 ] ;
13
47
48
+ function averageNumbers ( arr ) {
49
+ if ( arr == "" ) {
50
+ return null
51
+ }
52
+ let total = 0
53
+ for ( let i = 0 ; i < arr . length ; i ++ ) {
54
+ total += Number ( arr [ i ] ) ;
55
+ }
56
+ let avg = total / arr . length ;
57
+ return avg
58
+ }
59
+
60
+
61
+
14
62
// Level 2: Array of strings
15
63
const wordsArr = [ 'seat' , 'correspond' , 'linen' , 'motif' , 'hole' , 'smell' , 'smart' , 'chaos' , 'fuel' , 'palace' ] ;
16
64
65
+ function averageWordLength ( str ) {
66
+ if ( str == "" ) {
67
+ return null
68
+ }
69
+ let total = 0
70
+ for ( let i = 0 ; i < str . length ; i ++ ) {
71
+ total += Number ( str [ i ] . length ) ;
72
+ }
73
+ let avg = total / str . length ;
74
+ return avg
75
+ }
76
+
77
+
17
78
// Iteration #5: Unique arrays
18
79
const wordsUnique = [
19
80
'crab' ,
@@ -29,9 +90,31 @@ const wordsUnique = [
29
90
'bring'
30
91
] ;
31
92
93
+ function uniquifyArray ( arr ) {
94
+ if ( arr == "" ) {
95
+ return null
96
+ }
97
+ let i ,
98
+ len = arr . length ,
99
+ out = [ ] ,
100
+ obj = { } ;
101
+
102
+ for ( i = 0 ; i < len ; i ++ ) {
103
+ obj [ arr [ i ] ] = 0 ;
104
+ }
105
+ for ( i in obj ) {
106
+ out . push ( i ) ;
107
+ }
108
+ return out ;
109
+ }
110
+
111
+ console . log ( uniquifyArray ( words ) )
112
+
32
113
// Iteration #6: Find elements
33
114
const wordsFind = [ 'machine' , 'subset' , 'trouble' , 'starting' , 'matter' , 'eating' , 'truth' , 'disobedience' ] ;
34
115
116
+
117
+
35
118
// Iteration #7: Count repetition
36
119
const wordsCount = [
37
120
'machine' ,
@@ -70,4 +153,4 @@ const matrix = [
70
153
[ 20 , 69 , 36 , 41 , 72 , 30 , 23 , 88 , 34 , 62 , 99 , 69 , 82 , 67 , 59 , 85 , 74 , 4 , 36 , 16 ] ,
71
154
[ 20 , 73 , 35 , 29 , 78 , 31 , 90 , 1 , 74 , 31 , 49 , 71 , 48 , 86 , 81 , 16 , 23 , 57 , 5 , 54 ] ,
72
155
[ 1 , 70 , 54 , 71 , 83 , 51 , 54 , 69 , 16 , 92 , 33 , 48 , 61 , 43 , 52 , 1 , 89 , 19 , 67 , 48 ]
73
- ] ;
156
+ ] ;
0 commit comments