@@ -12,18 +12,8 @@ function maxOfTwoNumbers(firstNumber, secondNumber) {
12
12
console . log ( maxOfTwoNumbers ( 3 , 3 ) )
13
13
14
14
// Iteration #2: Find longest word
15
- /* const words = ['mystery', 'brother', 'aviator', 'crocodile', 'pearl', 'orchard', 'crackpot'];
16
- function findLongestWord(array) {
17
- let base = '';
18
- for (let i in array) {
19
- if (base.length < array[i].length) {
20
- base = array[i]
21
- }
22
- }
23
- return base;
24
- }
25
- */
26
15
const words = [ 'mystery' , 'brother' , 'aviator' , 'crocodile' , 'pearl' , 'orchard' , 'crackpot' ] ;
16
+ //case Expected '' to be null. doesn't work
27
17
function findLongestWord ( array ) {
28
18
let base = '' ;
29
19
if ( array !== [ ] ) {
@@ -32,15 +22,12 @@ function findLongestWord(array) {
32
22
base = array [ i ]
33
23
}
34
24
}
35
- return base ;
36
25
} else {
37
- return null ;
26
+ base = 0 ;
38
27
}
39
-
40
- }
41
-
42
-
43
- console . log ( findLongestWord ( words ) )
28
+ return base ;
29
+ }
30
+ console . log ( findLongestWord ( words ) )
44
31
45
32
// Iteration #3: Calculate the sum
46
33
@@ -73,41 +60,45 @@ console.log(sum(mixedArr))
73
60
74
61
// Iteration #4: Calculate the average
75
62
// Level 1: Array of numbers
63
+
64
+ //case "Calculate the average of an array of numbers should return null if receives an empty array when called" doesnt work
76
65
const numbersAvg = [ 2 , 6 , 9 , 10 , 7 , 4 , 1 , 9 ] ;
77
66
function averageNumbers ( numArrayAver ) {
78
67
let sumForAverage = 0 ;
79
- for ( let z = 0 ; z < numArrayAver . length ; z ++ ) {
80
- sumForAverage += numArrayAver [ z ]
81
- }
82
- if ( numArrayAver . length > 1 ) {
83
- let result = ( sumForAverage / ( numArrayAver . length - 1 ) ) . toFixed ( 0 )
84
- return result ;
68
+ if ( numArrayAver . length === 1 ) {
69
+ sumForAverage = numArrayAver [ 0 ] ;
85
70
} else {
86
- return sumForAverage
71
+ for ( let z = 0 ; z < numArrayAver . length ; z ++ ) {
72
+ sumForAverage += numArrayAver [ z ] ;
73
+ }
74
+ sumForAverage /= numArrayAver . length ;
87
75
}
88
-
76
+ return Number ( sumForAverage . toFixed ( 0 ) )
89
77
}
90
78
console . log ( averageNumbers ( numbersAvg ) ) ;
91
79
92
80
// Level 2: Array of strings
81
+
82
+ //case item.length === 0 doesn't workf
93
83
const wordsArr = [ 'seat' , 'correspond' , 'linen' , 'motif' , 'hole' , 'smell' , 'smart' , 'chaos' , 'fuel' , 'palace' ] ;
94
84
function averageWordLength ( item ) {
95
- let sumLength = 0 ;
96
- for ( let b = 0 ; b < item . length ; b ++ ) {
97
- if ( item . length > 1 ) {
98
- sumLength += item [ b ] . length ;
99
- return sumLength
100
- } else if ( item . length === 1 ) {
101
- return item [ b ] . length ;
102
- } else {
103
- return null ;
104
- }
85
+ if ( item . length === 0 ) {
86
+ return null ;
87
+ } else {
88
+ let sumLength = 0 ;
89
+ for ( let b = 0 ; b < item . length ; b ++ ) {
90
+ if ( item . length > 1 ) {
91
+ sumLength += item [ b ] . length ;
92
+
93
+ } else if ( item . length === 1 ) {
94
+ return item [ 0 ] . length ;
95
+ }
96
+ } return sumLength / item . length
105
97
}
106
-
107
98
}
108
-
109
99
console . log ( averageWordLength ( wordsArr ) )
110
100
101
+
111
102
// Iteration #5: Unique arrays
112
103
const wordsUnique = [
113
104
'crab' ,
0 commit comments