@@ -19,7 +19,7 @@ function findLongestWord(words) {
1919 return words [ 0 ]
2020 } else if ( words . length > 1 ) {
2121 words . sort ( function ( a , b ) {
22- if ( a . length < b . length ) return 1 ;
22+ if ( a . length < b . length ) return 1 ;
2323 if ( a . length > b . length ) return - 1 ;
2424 if ( a . length === b . length ) return 0 ;
2525 } ) ;
@@ -30,26 +30,45 @@ function findLongestWord(words) {
3030// Iteration #3: Calculate the sum
3131// const numbers = [6, 12, 1, 18, 13, 16, 2, 1, 8, 10];
3232
33- function sumNumbers ( numbers ) {
33+ function sumNumbers ( numbers ) {
3434 if ( numbers . length === 0 ) {
3535 return 0
3636 } else if ( numbers . length >= 1 ) {
3737 let sum = 0
38- for ( let i = 0 ; i < numbers . length ; ++ i )
38+ for ( let i = 0 ; i < numbers . length ; ++ i ) {
3939 sum += numbers [ i ] ;
40+ }
4041 return sum
41- }
42+
43+ }
4244}
4345
4446
4547
4648// Iteration #3.1 Bonus:
4749// const mixedArr = [6, 12, 'miami', 1, true, 'barca', '200', 'lisboa', 8, 10];
4850
49- function sum ( mixedArr ) {
50-
51+ function sum ( mixedArr ) {
52+ for ( const elements of mixedArr ) {
53+ if ( typeof elements === 'number' ) {
54+ let sum = 0
55+ for ( let i = 0 ; i < mixedArr . length ; ++ i ) {
56+ sum += mixedArr [ i ] ;
57+ }
58+ return sum
59+
60+ } else if ( typeof elements === 'string' ) {
61+ let sum2 = 0
62+ for ( let i = 0 ; i < mixedArr . length ; ++ i ) {
63+ sum2 += mixedArr [ i ] . length
64+ }
65+ return sum2
5166
67+ }
5268 }
69+ }
70+
71+
5372
5473
5574
0 commit comments