1
1
// Iteration #1: Find the maximum
2
2
3
+ function maxOfTwoNumbers ( num1 , num2 ) {
4
+ if ( num1 > num2 ) {
5
+ return console . log ( `O maior número é ${ num1 } ` ) ;
6
+ }
7
+ return console . log ( `O maior número é ${ num2 } ` ) ;
8
+ }
9
+
10
+ maxOfTwoNumbers ( 1 , 3 ) ;
3
11
4
12
// Iteration #2: Find longest word
5
13
const words = [ 'mystery' , 'brother' , 'aviator' , 'crocodile' , 'pearl' , 'orchard' , 'crackpot' ] ;
14
+ let longestWord = '' ;
15
+
16
+ function findLongestWord ( str ) {
17
+
18
+ for ( i = 0 ; i < str . length ; i ++ ) {
19
+ if ( str [ i ] . length > longestWord ) {
20
+ longestWord = str [ i ] . length ;
21
+ }
22
+ }
23
+ return `A maior palavra possui ${ longestWord } letras` ;
24
+ }
25
+
26
+ console . log ( findLongestWord ( words ) ) ;
27
+
28
+
6
29
7
30
// Iteration #3: Calculate the sum
8
31
9
32
const numbers = [ 6 , 12 , 1 , 18 , 13 , 16 , 2 , 1 , 8 , 10 ] ;
10
33
34
+ function sumNumbers ( sum ) {
35
+ let total = 0 ;
36
+ sum . forEach ( function ( element ) {
37
+ total += element ;
38
+ } ) ;
39
+ return total ;
40
+ }
41
+
42
+ console . log ( `A soma total dos números é ${ sumNumbers ( numbers ) } ` ) ;
43
+
44
+ const mixedArr = [ 6 , 12 , 'miami' , 1 , true , 'barca' , '200' , 'lisboa' , 8 , 10 ] ;
45
+
46
+
47
+
11
48
// Iteration #4: Calculate the average
12
49
// Level 1: Array of numbers
13
50
const numbersAvg = [ 2 , 6 , 9 , 10 , 7 , 4 , 1 , 9 ] ;
14
51
52
+
53
+ function averageNumbers ( number ) {
54
+ if ( ! number . length ) return ;
55
+
56
+ return sumNumbers ( number ) / number . length ;
57
+ }
58
+
59
+ console . log ( `A soma dos números é ${ sumNumbers ( numbersAvg ) } ` ) ;
60
+ console . log ( `A média dos números é ${ averageNumbers ( numbersAvg ) } ` ) ;
61
+
62
+
15
63
// Level 2: Array of strings
16
64
const wordsArr = [ 'seat' , 'correspond' , 'linen' , 'motif' , 'hole' , 'smell' , 'smart' , 'chaos' , 'fuel' , 'palace' ] ;
17
65
66
+ function averageWordLength ( str ) {
67
+
68
+ return str . join ( '' ) . length / str . length ;
69
+ }
70
+
71
+ console . log ( `A média do comprimento das palavras é ${ averageWordLength ( wordsArr ) } ` )
72
+
18
73
// Iteration #5: Unique arrays
19
74
const wordsUnique = [
20
75
'crab' ,
@@ -30,9 +85,17 @@ const wordsUnique = [
30
85
'bring'
31
86
] ;
32
87
88
+ console . log ( wordsUnique . filter ( ( item , index ) => wordsUnique . indexOf ( item ) === index ) )
89
+
33
90
// Iteration #6: Find elements
34
91
const wordsFind = [ 'machine' , 'subset' , 'trouble' , 'starting' , 'matter' , 'eating' , 'truth' , 'disobedience' ] ;
35
92
93
+ function doesWordExist ( arrayWords , wordSearch ) {
94
+ return arrayWords . includes ( wordSearch ) ;
95
+ }
96
+
97
+ console . log ( doesWordExist ( wordsFind , 'subset' ) )
98
+
36
99
// Iteration #7: Count repetition
37
100
const wordsCount = [
38
101
'machine' ,
@@ -48,6 +111,13 @@ const wordsCount = [
48
111
'matter'
49
112
] ;
50
113
114
+ function howManyTimes ( arrayWords , wordSearch ) {
115
+
116
+ return arrayWords . filter ( element => element === wordSearch ) . length
117
+ }
118
+
119
+ console . log ( howManyTimes ( wordsCount , 'matter' ) )
120
+
51
121
// Iteration #8: Bonus
52
122
53
123
const matrix = [
0 commit comments