@@ -13,9 +13,7 @@ function maxOfTwoNumbers(num1, num2) {
13
13
const words = [ 'mystery' , 'brother' , 'aviator' , 'crocodile' , 'pearl' , 'orchard' , 'crackpot' ] ;
14
14
15
15
function findLongestWord ( arrOfWords ) {
16
- if ( arrOfWords . length === 0 ) {
17
- return null ;
18
- }
16
+ if ( ! arrOfWords . length ) return null ;
19
17
20
18
let wordLngth = 0 ;
21
19
let longestWord ;
@@ -33,6 +31,8 @@ function findLongestWord(arrOfWords) {
33
31
const numbers = [ 6 , 12 , 1 , 18 , 13 , 16 , 2 , 1 , 8 , 10 ] ;
34
32
35
33
function sumNumbers ( arrNumbers ) {
34
+ if ( ! arrNumbers . length ) return 0 ;
35
+
36
36
let sum = 0 ;
37
37
38
38
for ( let i = 0 ; i < arrNumbers . length ; i ++ ) {
@@ -42,15 +42,30 @@ function sumNumbers(arrNumbers) {
42
42
}
43
43
44
44
// Iteration #3.1 Bonus:
45
- function sum ( arrOfNumbers ) {
46
- let sum = 0 ;
47
- let sumOfString = '' ;
48
- for ( let i = 0 ; i < arrOfNumbers . length ; i ++ ) {
49
- sum += arrOfNumbers [ i ] ;
45
+ function sum ( arrMixed ) {
46
+ if ( ! arrMixed . length ) return 0 ;
47
+
48
+ let sumMixed = 0 ;
49
+
50
+ // for of loop iterates over the array
51
+ // conditional statements delare what to do with different datatypes:
52
+ // if it is a string look at the length of it and add it to sum
53
+ // if it is a boolean ask if it is true or false. add 1 for true
54
+ // and 0 for false (? 1 : 0 could also be 10 : 20)
55
+
56
+ for ( const item of arrMixed ) {
57
+ if ( typeof item === 'string' ) {
58
+ sumMixed += item . length ;
59
+ } else if ( typeof item === 'boolean' ) {
60
+ sumMixed += item ? 1 : 0 ;
61
+ } else if ( typeof item === 'number' ) {
62
+ sumMixed += item ;
63
+ } else {
64
+ throw new Error ( "Unsupported data type sir or ma'am" ) ;
65
+ }
50
66
}
51
- return sum ;
67
+ return sumMixed ;
52
68
}
53
-
54
69
// Iteration #4: Calculate the average
55
70
// Level 1: Array of numbers
56
71
const numbersAvg = [ 2 , 6 , 9 , 10 , 7 , 4 , 1 , 9 ] ;
@@ -89,7 +104,31 @@ function averageWordLength(arr) {
89
104
}
90
105
91
106
// Bonus - Iteration #4.1
92
- function avg ( ) { }
107
+ function avg ( mixedArr ) {
108
+ if ( ! mixedArr . length ) return null ;
109
+
110
+ let sumMixed = 0 ;
111
+
112
+ // for of loop iterates over the array
113
+ // conditional statements delare what to do with different datatypes:
114
+ // if it is a string look at the length of it and add it to sum
115
+ // if it is a boolean ask if it is true or false. add 1 for true
116
+ // and 0 for false (? 1 : 0 could also be 10 : 20)
117
+
118
+ for ( const item of mixedArr ) {
119
+ if ( typeof item === 'string' ) {
120
+ sumMixed += item . length ;
121
+ } else if ( typeof item === 'boolean' ) {
122
+ sumMixed += item ? 1 : 0 ;
123
+ } else if ( typeof item === 'number' ) {
124
+ sumMixed += item ;
125
+ } else {
126
+ throw new Error ( "Unsupported data type sir or ma'am" ) ;
127
+ }
128
+ }
129
+ let toManyDigits = sumMixed / mixedArr . length ;
130
+ return + toManyDigits . toFixed ( 2 ) ;
131
+ }
93
132
94
133
// Iteration #5: Unique arrays
95
134
const wordsUnique = [
0 commit comments