11// Iteration #1: Find the maximum
2- function maxOfTwoNumbers ( ) { }
3-
2+ function maxOfTwoNumbers ( a , b ) {
3+ if ( a > b ) {
4+ return a ;
5+ } else {
6+ return b ;
7+ }
8+ }
49
510
611// Iteration #2: Find longest word
712const words = [ 'mystery' , 'brother' , 'aviator' , 'crocodile' , 'pearl' , 'orchard' , 'crackpot' ] ;
813
9- function findLongestWord ( ) { }
10-
11-
14+ function findLongestWord ( words ) {
15+ let longest = "" ;
16+ let number = 0 ;
17+ let arr = [ ] ;
18+ let max ;
19+ if ( words . length === 0 ) {
20+ return null ;
21+ }
22+
23+ words . forEach ( item => {
24+ arr . push ( item . length ) ;
25+ } )
26+ // calculate the maximum value of an array
27+ max = Math . max ( ...arr ) ;
28+ // iterates through 'arr' to find the first maximum length
29+
30+ for ( let i = 0 ; i < arr . length ; i ++ ) {
31+ // if the item is === max, return the value
32+ if ( arr [ i ] === max ) {
33+ longest = words [ i ] ;
34+ return longest ;
35+ }
36+ }
37+ }
1238
1339// Iteration #3: Calculate the sum
1440const numbers = [ 6 , 12 , 1 , 18 , 13 , 16 , 2 , 1 , 8 , 10 ] ;
1541
16- function sumNumbers ( ) { }
42+ function sumNumbers ( numbers ) {
43+ let sum = 0 ;
1744
45+ for ( let i = 0 ; i < numbers . length ; i ++ ) {
46+ sum += numbers [ i ] ;
47+ }
48+ return sum ;
49+ }
1850
51+ // Option 2
52+ // function sumNumbers(numbers) {
53+ // let counter = 0;
54+ // numbers.forEach(item => {
55+ // counter += item;
56+ // })
57+ // return counter;
58+ // }
59+
60+ // Option 3
61+ // function sumNumbers(numbers) {
62+ // // return the result of reduce
63+ // return numbers.reduce((a,b) => { // Explicit return, in more than 1 line, with {} and 'return' keyword
64+ // return a + b
65+ // },0);
66+ // }
67+
68+ // Option 3
69+ // function sumNumbers(numbers) {
70+ // // return the result of reduce
71+ // return numbers.reduce((a,b) => a + b, 0);// Implicit return, in one line without the word 'return'
72+ // }
1973
2074// Iteration #3.1 Bonus:
21- function sum ( ) { }
75+ //sum() that calculates the sum for array filled with (almost) any type of data.
76+ //Note that strings should have their length added to the total,
77+ //and boolean values should be coerced into their corresponding numeric values.
78+
79+
80+ function sum ( numbers ) {
81+ let counter = 0 ;
82+
83+ numbers . forEach ( item => {
84+ let notValid = typeof item === 'object' || Array . isArray ( item ) ;
85+ let isString = typeof item === 'string' ;
86+ let isBoolean = typeof item === 'boolean' ;
87+ let isNumber = typeof item === 'number' ;
88+
89+ if ( notValid ) {
90+ throw `Unsupported data type sir or ma'am` ;
91+ }
92+
93+ if ( isNumber ) {
94+ counter += item ;
95+ } else if ( isString ) {
96+ counter += item . length ;
97+ } else if ( isBoolean ) {
98+ if ( item === false ) {
99+ counter += 0 ;
100+ } else if ( item === true ) {
101+ counter += 1 ;
102+ }
103+ }
104+ } )
105+ return counter ;
106+ }
22107
23108
24109
25110// Iteration #4: Calculate the average
26111// Level 1: Array of numbers
27112const numbersAvg = [ 2 , 6 , 9 , 10 , 7 , 4 , 1 , 9 ] ;
28113
29- function averageNumbers ( ) { }
30114
115+ function averageNumbers ( numbers ) {
116+ //should return null if receives an empty array when called
117+ if ( numbers . length === 0 ) {
118+ return null ;
119+ }
120+ let totalNumbers = numbers . length ;
121+ let average ;
122+ let sum = sumNumbers ( numbers ) ;
123+
124+ average = sum / totalNumbers ;
125+ return average ;
126+ }
31127
32128// Level 2: Array of strings
33129const wordsArr = [ 'seat' , 'correspond' , 'linen' , 'motif' , 'hole' , 'smell' , 'smart' , 'chaos' , 'fuel' , 'palace' ] ;
34130
35- function averageWordLength ( ) { }
131+ function averageWordLength ( words ) {
132+ let arrWordsLength = [ ] ;
133+
134+ //iterate to create new array with length of words
135+ words . forEach ( item => {
136+ arrWordsLength . push ( item . length ) ;
137+ } ) ;
138+ // Average of numbers based on the Array of the strings
139+ let avgNumbers = averageNumbers ( arrWordsLength ) ;
140+ return avgNumbers ;
141+ }
142+
143+ // function averageWordLength(words) {
144+ // let sum = 0;
145+ // let arrWordsLength = [];
146+ // let totalItems = words.length;
147+ // let totalAverage;
148+
149+ // //iterate to create new array with length of words
150+ // words.forEach(item => {
151+ // arrWordsLength.push(item.length);
152+ // });
153+ // //console.log(arrWordsLength);
154+
155+ // //iterate to sum the length of numbers
156+ // //Option 2 -- forEach loop
157+ // arrWordsLength.forEach(item => {
158+ // sum += item;
159+ // });
160+
161+ // //Option 3 -- for loop
162+ // // for(let i = 0; i < arrWordsLength.length; i ++) {
163+ // // counter += arrWordsLength[i];
164+ // // //console.log(`counter: ${counter}`);
165+ // // }
166+ // totalAverage = sum/totalItems;
167+ // //console.log(totalAverage)
168+ // return totalAverage;
169+ // }
170+
171+ //const mixedArr = [6, 12, 'miami', 1, true, 'barca', '200', 'lisboa', 8, 10];
172+ // Expected --> 5.7
173+ // But there is an error on the tests--> expected 5.1
36174
37175// Bonus - Iteration #4.1
38- function avg ( ) { }
176+ function avg ( arr ) {
177+ let totalItems = arr . length ;
178+ // check if there is an empty array
179+ if ( arr . length === 0 ) {
180+ return null ;
181+ }
182+ // Use Function Sum to iterate over the items and sum mixed items
183+ let sumMixedItems = sum ( arr ) ;
184+ let avgMixedItems = sumMixedItems / totalItems ;
185+ return avgMixedItems ;
186+ }
39187
40188// Iteration #5: Unique arrays
41189const wordsUnique = [
@@ -52,17 +200,45 @@ const wordsUnique = [
52200 'bring'
53201] ;
54202
55- function uniquifyArray ( ) { }
56-
57-
203+ //remove the duplicates, and return a new array.
204+ //You are more than likely going to want to check out the indexOf Array method.
205+
206+ function uniquifyArray ( words ) {
207+ if ( words . length === 0 ) {
208+ return null ;
209+ }
210+ let unique = [ ] ;
211+
212+ words . forEach ( word => {
213+ //console.log(`item: ${item}`);
214+ // Checks if the word it's not included inside the array given 'words'
215+ const isWordRepeated = unique . includes ( word ) ;
216+ if ( ! isWordRepeated ) { //is not included
217+ unique . push ( word ) ;
218+ }
219+ } )
220+ return unique ;
221+ }
58222
59223// Iteration #6: Find elements
60224const wordsFind = [ 'machine' , 'subset' , 'trouble' , 'starting' , 'matter' , 'eating' , 'truth' , 'disobedience' ] ;
61-
62- function doesWordExist ( ) { }
63-
64-
65-
225+ //that will take in an array of words as one argument,
226+ //and a word to search for as the other.
227+ //Return true if it exists, otherwise, return false.
228+ //that will take in an array of words as one argument, and a word to search for as the other. Return true if it exists, otherwise, return false. Don't use indexOf for this one.
229+ //Don't use indexOf for this one.
230+ function doesWordExist ( arrWords , word ) {
231+ // return null if the array is empty
232+ if ( arrWords . length === 0 ) {
233+ return null ;
234+ }
235+ // if the word is included into the array --> true
236+ if ( arrWords . includes ( word ) ) {
237+ return true ;
238+ } else { // if the word is not included inside the array --> false
239+ return false ;
240+ }
241+ }
66242// Iteration #7: Count repetition
67243const wordsCount = [
68244 'machine' ,
@@ -77,8 +253,25 @@ const wordsCount = [
77253 'disobedience' ,
78254 'matter'
79255] ;
80-
81- function howManyTimes ( ) { }
256+ // Iteration #7: Count repetition
257+ function howManyTimes ( arrWords , wordToCheck ) {
258+ // if the array is empty, return 0
259+ if ( arrWords . length === 0 ) {
260+ return 0 ;
261+ }
262+ let counter = 0 ;
263+ //iterates to check if the wordToCheck is included inside the array of words
264+ arrWords . forEach ( word => {
265+ //let isWordIncluded = word.includes(word);
266+ let isWordIncluded = word === wordToCheck ;
267+ if ( isWordIncluded ) {
268+ counter += 1 ;
269+ }
270+ } )
271+ return counter ;
272+ }
273+ //howManyTimes(wordsCount, "matter")
274+ // 4
82275
83276
84277
@@ -99,7 +292,7 @@ const matrix = [
99292 [ 86 , 56 , 0 , 48 , 35 , 71 , 89 , 7 , 5 , 44 , 44 , 37 , 44 , 60 , 21 , 58 , 51 , 54 , 17 , 58 ] ,
100293 [ 19 , 80 , 81 , 68 , 5 , 94 , 47 , 69 , 28 , 73 , 92 , 13 , 86 , 52 , 17 , 77 , 4 , 89 , 55 , 40 ] ,
101294 [ 4 , 52 , 8 , 83 , 97 , 35 , 99 , 16 , 7 , 97 , 57 , 32 , 16 , 26 , 26 , 79 , 33 , 27 , 98 , 66 ] ,
102- [ 88 , 36 , 68 , 87 , 57 , 62 , 20 , 72 , 3 , 46 , 33 , 67 , 46 , 55 , 12 , 32 , 63 , 93 , 53 , 69 ] ,
295+ [ 88 , 36 , 68 , 87 , 57 , 62 , 20 , 72 , 3 , 46 , 33 , 67 , 46 , 55 , 412 , 32 , 63 , 93 , 53 , 69 ] ,
103296 [ 4 , 42 , 16 , 73 , 38 , 25 , 39 , 11 , 24 , 94 , 72 , 18 , 8 , 46 , 29 , 32 , 40 , 62 , 76 , 36 ] ,
104297 [ 20 , 69 , 36 , 41 , 72 , 30 , 23 , 88 , 34 , 62 , 99 , 69 , 82 , 67 , 59 , 85 , 74 , 4 , 36 , 16 ] ,
105298 [ 20 , 73 , 35 , 29 , 78 , 31 , 90 , 1 , 74 , 31 , 49 , 71 , 48 , 86 , 81 , 16 , 23 , 57 , 5 , 54 ] ,
0 commit comments