Skip to content

Commit 680a021

Browse files
committed
ITERATION ironhack-labs#3.1 Bonus. DONE
1 parent aec0043 commit 680a021

File tree

1 file changed

+24
-12
lines changed

1 file changed

+24
-12
lines changed

src/functions-and-arrays.js

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,13 @@ function maxOfTwoNumbers(a, b) {
99
if (a > b) {
1010
return a;
1111
} else {
12-
return b;
12+
return b;
1313
}
1414
}
1515
console.log(maxOfTwoNumbers(5, 8))
1616

17-
1817
//////////////////////////////////////////////////
1918

20-
2119
// Iteration #2: Find longest word =========== > DONE: Si el array está vacio sale null
2220
const words = ['mystery', 'brother', 'aviator', 'crocodile', 'pearl', 'orchard', 'crackpot'];
2321

@@ -38,29 +36,40 @@ function findLongestWord(arr) {
3836

3937
console.log(findLongestWord([]))
4038

41-
4239
///////////////////////////////////////////////////
4340

44-
45-
4641
// Iteration #3: Calculate the sum =========== > ME FALTA: Si el array está vacio
4742
const numbers = [6, 12, 1, 18, 13, 16, 2, 1, 8, 10];
4843

4944
function sumNumbers(array){
5045
let sum = 0;
5146

5247
for (let i = 0; i < array.length; i++) {
53-
sum += array[i];
54-
}
55-
return sum
48+
sum += array[i];
49+
} return sum
5650
}
5751
console.log(sumNumbers([9,8]))
5852

59-
6053
///////////////////////////////////////////////////
6154

55+
//const mixedArr = [6, 12, 'miami', 1, true, 'barca', '200', 'lisboa', 8, 10];
56+
// should return: 57
57+
6258
// Iteration #3.1 Bonus:
63-
function sum() {}
59+
function sum(array) {
60+
let result = 0
61+
for (let i = 0; i < array.length; i++) {
62+
if (typeof elementInArray === 'string'){
63+
result += array[i].lenght
64+
} else if (typeof array[i] === 'string') {
65+
result += array[i].length
66+
} else if (typeof array[i] === 'boolean') {
67+
if (array[i] === true) {result++}
68+
}
69+
}
70+
return result
71+
}
72+
console.log(sum([6, 12, 'miami', 1, true, 'barca', '200', 'lisboa', 8, 10]))
6473

6574
///////////////////////////////////////////////////
6675

@@ -75,6 +84,7 @@ function averageNumbers(arr) {
7584
countNumbers += 1
7685
}
7786
return sumNumbers(arr) / countNumbers
87+
7888
}
7989

8090
console.log(averageNumbers([10,20]))
@@ -85,7 +95,9 @@ function averageNumbers(arr) {
8595
// Level 2: Array of strings
8696
const wordsArr = ['seat', 'correspond', 'linen', 'motif', 'hole', 'smell', 'smart', 'chaos', 'fuel', 'palace'];
8797

88-
function averageWordLength() { }
98+
function averageWordLength(array) {
99+
100+
}
89101

90102
// Bonus - Iteration #4.1
91103
function avg() {}

0 commit comments

Comments
 (0)