@@ -7,24 +7,23 @@ function maxOfTwoNumbers(num1, num2) {
77 }
88}
99maxOfTwoNumbers ( 3 , 2 ) ;
10-
1110//Iteration #2: Find longest word
1211const words = [ 'mystery' , 'brother' , 'aviator' , 'crocodile' , 'pearl' , 'orchard' , 'crackpot' ] ;
13-
1412function findLongestWord ( array ) {
1513 let longestWord = null ;
16- for ( let i = 0 ; i < array . length ; i ++ ) {
17- if ( array [ i ] . length > longestWord . length ) {
18- longestWord = array [ i ] ;
14+ if ( array . length > 0 ) {
15+ longestWord = " " ;
16+ for ( let i = 0 ; i < array . length ; i ++ ) {
17+ if ( array [ i ] . length > longestWord . length ) {
18+ longestWord = array [ i ] ;
19+ }
1920 }
2021 }
2122 return longestWord ;
2223}
2324findLongestWord ( words ) ;
24-
25- // Iteration #3: Calculate the sum
25+ /// Iteration #3: Calculate the sum
2626const numbers = [ 6 , 12 , 1 , 18 , 13 , 16 , 2 , 1 , 8 , 10 ] ;
27-
2827function sumNumbers ( arrayOfNumbers ) {
2928 let result = 0 ;
3029 for ( i = 0 ; i < arrayOfNumbers . length ; i ++ ) {
@@ -33,35 +32,38 @@ function sumNumbers(arrayOfNumbers) {
3332 return result ;
3433}
3534sumNumbers ( numbers ) ;
36-
3735// Iteration #3.1 Bonus:
3836function sum ( ) { }
39-
4037// Iteration #4: Calculate the average
4138// Level 1: Array of numbers
39+ // What do we do with the sum? what does the number of elements contain
4240const numbersAvg = [ 2 , 6 , 9 , 10 , 7 , 4 , 1 , 9 ] ;
43-
4441function averageNumbers ( elements ) {
45- const calc = sumNumbers ( elements ) / elements . length ;
46- return calc ;
42+ let calc = null ;
43+ if ( elements . length > 0 ) {
44+ calc = sumNumbers ( elements ) / elements . length ;
45+ }
46+ return calc ;
4747}
4848averageNumbers ( numbersAvg ) ;
49-
5049// Level 2: Array of strings
50+ // What does the arrayofwords contain? Should I return the length of all words in the array?
5151const wordsArr = [ 'seat' , 'correspond' , 'linen' , 'motif' , 'hole' , 'smell' , 'smart' , 'chaos' , 'fuel' , 'palace' ] ;
5252function averageWordLength ( words ) {
53- let totalCharacters = 0 ;
54- for ( let i = 0 ; i < words . length ; i ++ ) {
55- totalCharacters += words [ i ] . length ;
56- }
57- return totalCharacters / words . length ;
53+ let totalChar = null ;
54+ if ( words . length > 0 ) {
55+ for ( let i = 0 ; i < words . length ; i ++ ) {
56+ totalChar += words [ i ] . length ;
57+ }
58+ totalChar = totalChar / words . length ;
59+ }
60+ return totalChar ;
5861 }
59- averageWordLength ( wordsArr ) ;
60-
62+ averageWordLength ( wordsArr )
6163// Bonus - Iteration #4.1
6264// return array without duplicates
65+ //
6366function avg ( ) { }
64-
6567// Iteration #5: Unique arrays
6668const wordsUnique = [
6769 'crab' ,
@@ -77,28 +79,35 @@ const wordsUnique = [
7779 'bring'
7880] ;
7981function uniquifyArray ( array ) {
80- let uniqueArr = [ ] ;
81- for ( let i = 0 ; i < array . length ; i ++ ) {
82- if ( uniqueArr . indexOf ( array [ i ] ) === - 1 ) {
83- uniqueArr . push ( array [ i ] )
82+ let uniqueArr = [ ] ;
83+ if ( array . length === 0 ) {
84+ uniqueArr = null ;
85+ }
86+ if ( array . length > 0 ) {
87+ for ( let i = 0 ; i < array . length ; i ++ ) {
88+ if ( uniqueArr . indexOf ( array [ i ] ) === - 1 ) {
89+ uniqueArr . push ( array [ i ] ) ;
90+ }
8491 }
8592 }
8693 return uniqueArr ;
8794}
8895uniquifyArray ( wordsUnique ) ;
89-
9096// Iteration #6: Find elements
9197const wordsFind = [ 'machine' , 'subset' , 'trouble' , 'starting' , 'matter' , 'eating' , 'truth' , 'disobedience' ] ;
9298function doesWordExist ( wordsArr , word ) {
93- if ( wordsArr . includes ( word ) ) {
94- return true ;
95- } else {
96- return false ;
99+ let wordIsOk = null ;
100+ if ( wordsArr . length > 0 ) {
101+ if ( wordsArr . includes ( word ) ) {
102+ wordIsOk = true ;
103+ } else {
104+ wordIsOk = false ;
105+ }
97106 }
107+ return wordIsOk ;
98108}
99- doesWordExist ( wordsFind , "monkey" ) ;
100- doesWordExist ( wordsFind , "subset" ) ;
101-
109+ doesWordExist ( wordsFind , "monkey" )
110+ doesWordExist ( wordsFind , "subset" )
102111// Iteration #7: Count repetition
103112const nullTestArray = [ ]
104113const wordsCount = [
@@ -121,11 +130,10 @@ function howManyTimes(repeatingWords, thisWord ) {
121130 wordCounter ++
122131 }
123132 }
124- return wordCounter ;
133+ return wordCounter
125134}
126- howManyTimes ( wordsCount , "trouble" ) ;
127- howManyTimes ( nullTestArray , [ ] ) ;
128-
135+ howManyTimes ( wordsCount , "trouble" )
136+ howManyTimes ( nullTestArray , [ ] )
129137// Iteration #8: Bonus
130138const matrix = [
131139 [ 8 , 2 , 22 , 97 , 38 , 15 , 0 , 40 , 0 , 75 , 4 , 5 , 7 , 78 , 52 , 12 , 50 , 77 , 91 , 8 ] ,
@@ -150,7 +158,6 @@ const matrix = [
150158 [ 1 , 70 , 54 , 71 , 83 , 51 , 54 , 69 , 16 , 92 , 33 , 48 , 61 , 43 , 52 , 1 , 89 , 19 , 67 , 48 ]
151159] ;
152160function greatestProduct ( ) { }
153-
154161// The following is required to make unit tests work.
155162/* Environment setup. Do not modify the below code. */
156163if ( typeof module !== 'undefined' ) {
@@ -167,4 +174,4 @@ if (typeof module !== 'undefined') {
167174 howManyTimes,
168175 greatestProduct
169176 } ;
170- } ;
177+ } ;
0 commit comments