1
- // Iteration #1: Find the maximum
2
- /* function maxOfTwoNumbers (num1,num2){
3
- if(num1 > num2){
4
- return num1
5
- }
6
- return num2
7
- }
8
- maxOfTwoNumbers(4,4)
9
- */
10
-
11
- // Iteration #2: Find longest word
12
- /* const words = ['mystery', 'brother', 'aviator', 'crocodile', 'pearl', 'orchard', 'crackpot'];
13
-
14
- function findLongestWord(arr){
15
- let wordLength = '';
16
- if(arr.length == ''){
17
- return null
18
- }
19
- for (let i=0;i<arr.length;i++){
20
- if(arr[i].length > wordLength.length){
21
- wordLength = arr[i]
22
- }
23
- }
24
- return wordLength
25
- }
26
- findLongestWord(words) */
27
-
28
- // Iteration #3: Calculate the sum
29
-
30
- /* const numbers = [6, 12, 1, 18, 13, 16, 2, 1, 8, 10];
31
- function sumNumbers(arr){
32
- let sum = 0;
33
- if (arr.length == ''){
34
- return 0
35
- }
36
- for (let i = 0; i < arr.length; i++) {
37
- sum+= arr[i]
38
- }
39
- return sum
40
- }
41
- sumNumbers(numbers)
42
-
43
- */
44
- // Iteration #4: Calculate the average
45
- // Level 1: Array of numbers
46
- /* const numbersAvg = [2, 6, 9, 10, 7, 4, 1, 9];
47
- function averageNumbers(arr){
48
- let sum = 0;
49
- if (arr.length == ''){
50
- return null
51
- }
52
- for (let i in arr) {
53
- sum+= arr[i]
54
- }
55
- return sum / arr.length
56
- }
57
- averageNumbers(numbersAvg)
58
- */
59
- // Level 2: Array of strings
60
- const wordsArr = [ 'seat' , 'correspond' , 'linen' , 'motif' , 'hole' , 'smell' , 'smart' , 'chaos' , 'fuel' , 'palace' ] ;
1
+ // // Iteration #1: Find the maximum
2
+ // function maxOfTwoNumbers (num1,num2){
3
+ // if(num1 > num2 || num1 == num2){
4
+ // return num1
5
+ // }
6
+ // return num2
7
+ // }
8
+ // maxOfTwoNumbers(4,4)
61
9
62
- /* function averageWordLength(arr){
63
- let sum = '';
64
-
65
- if (arr.length == ''){
66
- return null
67
- }
10
+
11
+
12
+ // // Iteration #2: Find longest word
13
+ // const words = ['mystery', 'brother', 'aviator', 'crocodile', 'pearl', 'orchard', 'crackpot'];
14
+
15
+ // function findLongestWord(arr){
16
+ // let wordLength = '';
17
+ // if(arr.length == ''){
18
+ // return null
19
+ // }
20
+ // for (let i=0;i<arr.length;i++){
21
+ // if(arr[i].length > wordLength.length){
22
+ // wordLength = arr[i]
23
+ // }
24
+ // }
25
+ // return wordLength
26
+ // }
27
+ // findLongestWord(words)
28
+
29
+ // // Iteration #3: Calculate the sum
30
+
31
+ // const numbers = [6, 12, 1, 18, 13, 16, 2, 1, 8, 10];
32
+
33
+ // function sumNumbers(arr){
34
+ // let sum = 0;
35
+ // if (arr.length == ''){
36
+ // return 0
37
+ // }
38
+ // for(let i = 0; i<arr.length; i++){
39
+ // sum += arr[i]
40
+ // }
41
+ // return sum
42
+ // }
43
+
44
+
45
+ // // bonus
46
+
47
+ // function sum(arr){
48
+ // let num = 0
49
+ // let str = 0
50
+ // let boo = 0
51
+ // for(let ele of arr){
52
+ // if (ele * 1 === ele){
53
+ // num += ele
54
+ // console.log(ele)
55
+ // }
56
+ // else if(typeof ele == 'string'){
57
+ // str += ele.length
58
+ // console.log(ele, 'is a string', ele.length)
59
+ // }
60
+ // else if( typeof ele == 'boolean' && ele == true){
61
+ // boo ++
62
+ // console.log(ele, 'is a string', ele.length)
63
+ // }
64
+ // else if (typeof ele == 'object') {
65
+ // throw new Error ("Unsupported data type sir or ma'am")
66
+ // }
67
+ // }
68
+ // return num + str + boo
69
+ // }
70
+ // sum(arr)
71
+
72
+
73
+ // //Iteration #4: Calculate the average
74
+ // //Level 1: Array of numbers
75
+ // const numbersAvg = [2, 6, 9, 10, 7, 4, 1, 9];
76
+ // function averageNumbers(arr){
77
+ // let sum = 0;
78
+ // if (arr.length == ''){
79
+ // return null
80
+ // }
81
+ // for(let ele of arr){
82
+ // sum += ele
83
+ // }
84
+ // return sum / arr.length
85
+ // }
86
+ // averageNumbers(numbersAvg)
87
+
88
+ // //Level 2: Array of strings
89
+ // const wordsArr = ['seat', 'correspond', 'linen', 'motif', 'hole', 'smell', 'smart', 'chaos', 'fuel', 'palace'];
90
+
91
+ // function averageWordLength(arr){
92
+ // let sum = 0;
68
93
69
- for (let i in arr){
70
-
71
- for (let j in arr[i]) {
72
- sum+= arr[i][j]
73
- sum.length
74
- }
94
+ // if (arr.length == ''){
95
+ // return null
96
+ // }
97
+ // for (let ele of arr) {
98
+ // sum += ele.length
99
+ // }
100
+ // return sum / arr.length
101
+ // }
102
+ // averageWordLength(wordsArr)
103
+
104
+
105
+ //Bonus - Iteration #4.1: A generic avg() function
106
+
107
+
108
+ // function avg(arr){
109
+ // let num = 0
110
+ // let str = 0
111
+ // let boo = 0
112
+ // let avg = 0
113
+ // if( arr.length == 0){
114
+ // return null
115
+ // }
116
+ // else{
117
+ // for(let ele of arr){
118
+ // if (ele * 1 === ele){
119
+ // num += ele
120
+ // //console.log(ele)
121
+ // }
122
+ // else if(typeof ele == 'string'){
123
+ // str += ele.length
124
+ // //console.log(ele, 'is a string', ele.length)
125
+ // }
126
+ // else if( typeof ele == 'boolean' && ele == true){
127
+ // boo ++
128
+ // //console.log(ele, 'is a string', ele.length)
129
+ // }
130
+ // }
131
+ // avg = ((num + str + boo)/ arr.length).toFixed(2)
132
+
133
+ // }
134
+ // //console.log(Number(avg))
135
+ // return Number(avg)
136
+ // }
137
+
138
+ // //Iteration #5: Unique arrays
139
+ // const wordsUnique = [
140
+ // 'crab',
141
+ // 'poison',
142
+ // 'contagious',
143
+ // 'simple',
144
+ // 'bring',
145
+ // 'sharp',
146
+ // 'playground',
147
+ // 'poison',
148
+ // 'communion',
149
+ // 'simple',
150
+ // 'bring'
151
+ // ];
75
152
76
- }
77
- return sum.length / arr.length
78
- }
79
- averageWordLength(wordsArr) */
80
- // Iteration #5: Unique arrays
81
- const wordsUnique = [
82
- 'crab' ,
83
- 'poison' ,
84
- 'contagious' ,
85
- 'simple' ,
86
- 'bring' ,
87
- 'sharp' ,
88
- 'playground' ,
89
- 'poison' ,
90
- 'communion' ,
91
- 'simple' ,
92
- 'bring'
93
- ] ;
94
- /*
95
- function uniquifyArray(arr) {
96
- const newArr = [];
97
- if(arr.length == 0){
98
- return null
99
- }
100
- for(let i=0; i< arr.length; i++){
101
- if(newArr.indexOf(arr[i]) < 0){
102
- newArr.push(arr[i])
103
- }
104
- }
105
- return newArr
106
- }
107
- uniquifyArray(wordsUnique)
108
- /*
109
- function uniquifyArray(arr) {
110
- if (arr.length == ''){
111
- return null
112
- }
113
- }
114
- uniquifyArray(wordsUnique)
115
- */
116
- // Iteration #6: Find elements
117
- const wordsFind = [ 'machine' , 'subset' , 'trouble' , 'starting' , 'matter' , 'eating' , 'truth' , 'disobedience' ] ;
118
-
119
- // Iteration #7: Count repetition
120
- const wordsCount = [
121
- 'machine' ,
122
- 'matter' ,
123
- 'subset' ,
124
- 'trouble' ,
125
- 'starting' ,
126
- 'matter' ,
127
- 'eating' ,
128
- 'matter' ,
129
- 'truth' ,
130
- 'disobedience' ,
131
- 'matter'
132
- ] ;
153
+
154
+ // function uniquifyArray(arr) {
155
+ // let newArr = [];
156
+ // if (arr.length == ''){
157
+ // return null
158
+ // }
159
+ // for (let i = 0; i < arr.length; i++) {
160
+ // if(newArr.indexOf(arr[i]) == -1){
161
+ // newArr.push(arr[i])
162
+ // }
163
+ // }
164
+ // return newArr
165
+ // }
166
+ // uniquifyArray(wordsUnique);
167
+
168
+
169
+ // //Iteration #6: Find elements
170
+ // const wordsFind = [
171
+ // 'machine',
172
+ // 'subset',
173
+ // 'trouble',
174
+ // 'starting',
175
+ // 'matter',
176
+ // 'eating',
177
+ // 'truth',
178
+ // 'disobedience'
179
+ // ];
180
+
181
+ // function doesWordExist(arr, word){
182
+
183
+ // if (arr.length == ''){
184
+ // return null
185
+ // }
186
+ // for (let i = 0; i < arr.length; i++) {
187
+ // if(arr[i] == word){
188
+ // return true
189
+ // }
190
+ // return false
191
+ // }
192
+ // return result
193
+ // }
194
+
195
+
196
+ // //Iteration #7: Count repetition
197
+ // const wordsCount = [
198
+ // 'machine',
199
+ // 'matter',
200
+ // 'subset',
201
+ // 'trouble',
202
+ // 'starting',
203
+ // 'matter',
204
+ // 'eating',
205
+ // 'matter',
206
+ // 'truth',
207
+ // 'disobedience',
208
+ // 'matter'
209
+ // ];
210
+
211
+ // function howManyTimes(arr, word) {
212
+ // let count = 0
213
+ // if (arr.length == ''){
214
+ // return count = 0
215
+ // }
216
+ // for (let i = 0; i < arr.length; i++) {
217
+ // if(arr[i] == word){
218
+ // count += 1
219
+ // }
220
+ // }
221
+ // return count
222
+ // }
133
223
134
224
// Iteration #8: Bonus
135
225
@@ -155,3 +245,9 @@ const matrix = [
155
245
[ 20 , 73 , 35 , 29 , 78 , 31 , 90 , 1 , 74 , 31 , 49 , 71 , 48 , 86 , 81 , 16 , 23 , 57 , 5 , 54 ] ,
156
246
[ 1 , 70 , 54 , 71 , 83 , 51 , 54 , 69 , 16 , 92 , 33 , 48 , 61 , 43 , 52 , 1 , 89 , 19 , 67 , 48 ]
157
247
] ;
248
+
249
+
250
+ function greatestProduct ( arr ) {
251
+ console . log ( arr [ 8 ] . length )
252
+ }
253
+ greatestProduct ( matrix )
0 commit comments