@@ -14,26 +14,21 @@ const words = ['mystery', 'brother', 'aviator', 'crocodile', 'pearl', 'orchard',
1414
1515function findLongestWord ( words ) {
1616 let longestWord = '' ;
17- let longestWordNumber = 0 ;
1817 if ( words . length === 1 || typeof words === "string" ) {
1918 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+ }
3225 }
33- }
26+ return longestWord
27+ } else if ( words . length === 0 || typeof words !== "object" ) {
28+ return null
29+ }
3430}
3531
36-
3732// Iteration #3: Calculate the sum
3833const numbers = [ 6 , 12 , 1 , 18 , 13 , 16 , 2 , 1 , 8 , 10 ] ;
3934
@@ -58,27 +53,34 @@ function sumNumbers(numbers) {
5853
5954
6055// Iteration #3.1 Bonus:
61- function sum ( numbers ) {
56+ const numbers2 = [ 10 , 5 , 4 , 32 , 8 ] //59
57+
58+ function sum ( numbers2 ) {
6259 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+ }
7579 }
76- return theTotal ;
80+ return theTotal
7781 }
7882}
7983
80-
81- // Iteration #4: Calculate the average
8284// Iteration #4: Calculate the average
8385// Level 1: Array of numbers
8486const numbersAvg = [ 2 , 6 , 9 , 10 , 7 , 4 , 1 , 9 ] ;
@@ -124,7 +126,7 @@ function avg(mixArr) {
124126 return null
125127 } else if ( mixArr . length > 1 ) {
126128 for ( let i = 0 ; i < mixArr . length ; i ++ ) {
127- if ( typeof mixArr [ i ] === 'number' ) {
129+ if ( typeof mixArr [ i ] === 'number' ) {
128130 theAverageWordsArr += mixArr [ i ]
129131 } else if ( typeof mixArr [ i ] === 'string' ) {
130132 theAverageWordsArr += mixArr [ i ] . length
0 commit comments