@@ -14,26 +14,21 @@ const words = ['mystery', 'brother', 'aviator', 'crocodile', 'pearl', 'orchard',
14
14
15
15
function findLongestWord ( words ) {
16
16
let longestWord = '' ;
17
- let longestWordNumber = 0 ;
18
17
if ( words . length === 1 || typeof words === "string" ) {
19
18
return words [ 0 ]
20
- }
21
- else if ( words . length === 0 || typeof words !== "object" ) {
22
- return null
23
- } else {
24
- for ( let i = 0 ; i < words . length - 1 ; i ++ ) {
25
- if ( words [ i ] . length === words [ i + 1 ] . length ) {
26
- return words [ i ]
27
- } else if ( words [ i ] . length > words [ i + 1 ] . length && words [ i ] . length > longestWordNumber ) {
28
- let longestWordNumber = words [ i ] . length ;
29
- let longestWord = words [ i ] ;
30
- return longestWord
31
- }
19
+ } else if ( words . length > 1 ) {
20
+ for ( let i = 0 ; i < words . length ; i ++ ) {
21
+ let individualWord = words [ i ]
22
+ if ( individualWord . length > longestWord . length ) {
23
+ longestWord = individualWord
24
+ }
32
25
}
33
- }
26
+ return longestWord
27
+ } else if ( words . length === 0 || typeof words !== "object" ) {
28
+ return null
29
+ }
34
30
}
35
31
36
-
37
32
// Iteration #3: Calculate the sum
38
33
const numbers = [ 6 , 12 , 1 , 18 , 13 , 16 , 2 , 1 , 8 , 10 ] ;
39
34
@@ -58,27 +53,34 @@ function sumNumbers(numbers) {
58
53
59
54
60
55
// Iteration #3.1 Bonus:
61
- function sum ( numbers ) {
56
+ const numbers2 = [ 10 , 5 , 4 , 32 , 8 ] //59
57
+
58
+ function sum ( numbers2 ) {
62
59
let theTotal = 0 ;
63
- let result = 0 ;
64
- if ( numbers . length === 1 ) {
65
- return numbers [ 0 ]
66
- } else if ( numbers . length === 0 || numbers === [ ] ) {
67
- return 0
68
- } else if ( numbers . length === 2 ) {
69
- let result = numbers [ 0 ] + numbers [ 1 ]
70
- return result
71
- } else if ( numbers . length > 2 || typeof numbers !== "number" ) {
72
- let result = 0 ;
73
- for ( let i = 0 ; i < numbers . length ; i ++ ) {
74
- theTotal += numbers [ i ] ;
60
+ if ( numbers2 . length === 0 ) {
61
+ theTotal = 0
62
+ return theTotal
63
+ } else if ( numbers2 . length === 1 ) {
64
+ theTotal = numbers2 [ 0 ]
65
+ return theTotal
66
+ } else if ( numbers2 . length > 0 ) {
67
+ for ( let i = 0 ; i < numbers2 . length ; i ++ ) {
68
+ if ( numbers2 [ i ] . length === 0 || typeof numbers2 [ i ] === 'object' ) {
69
+ throw theTotal = `Unsupported data type sir or ma'am` ;
70
+ } else if ( typeof numbers2 [ i ] === 'number' ) {
71
+ theTotal += numbers2 [ i ]
72
+ } else if ( typeof numbers2 [ i ] === 'string' ) {
73
+ theTotal += numbers2 [ i ] . length
74
+ } else if ( typeof numbers2 [ i ] === 'boolean' && numbers2 [ i ] === true ) {
75
+ theTotal += 1
76
+ } else if ( typeof numbers2 [ i ] === 'boolean' && numbers2 [ i ] === false ) {
77
+ theTotal += 0
78
+ }
75
79
}
76
- return theTotal ;
80
+ return theTotal
77
81
}
78
82
}
79
83
80
-
81
- // Iteration #4: Calculate the average
82
84
// Iteration #4: Calculate the average
83
85
// Level 1: Array of numbers
84
86
const numbersAvg = [ 2 , 6 , 9 , 10 , 7 , 4 , 1 , 9 ] ;
@@ -124,7 +126,7 @@ function avg(mixArr) {
124
126
return null
125
127
} else if ( mixArr . length > 1 ) {
126
128
for ( let i = 0 ; i < mixArr . length ; i ++ ) {
127
- if ( typeof mixArr [ i ] === 'number' ) {
129
+ if ( typeof mixArr [ i ] === 'number' ) {
128
130
theAverageWordsArr += mixArr [ i ]
129
131
} else if ( typeof mixArr [ i ] === 'string' ) {
130
132
theAverageWordsArr += mixArr [ i ] . length
0 commit comments