Skip to content

Commit 1929824

Browse files
author
Martin
committed
solved all but bonus ironhack-labs#8
1 parent f4a13cf commit 1929824

File tree

1 file changed

+50
-11
lines changed

1 file changed

+50
-11
lines changed

src/functions-and-arrays.js

Lines changed: 50 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,7 @@ function maxOfTwoNumbers(num1, num2) {
1313
const words = ['mystery', 'brother', 'aviator', 'crocodile', 'pearl', 'orchard', 'crackpot'];
1414

1515
function findLongestWord(arrOfWords) {
16-
if (arrOfWords.length === 0) {
17-
return null;
18-
}
16+
if (!arrOfWords.length) return null;
1917

2018
let wordLngth = 0;
2119
let longestWord;
@@ -33,6 +31,8 @@ function findLongestWord(arrOfWords) {
3331
const numbers = [6, 12, 1, 18, 13, 16, 2, 1, 8, 10];
3432

3533
function sumNumbers(arrNumbers) {
34+
if (!arrNumbers.length) return 0;
35+
3636
let sum = 0;
3737

3838
for (let i = 0; i < arrNumbers.length; i++) {
@@ -42,15 +42,30 @@ function sumNumbers(arrNumbers) {
4242
}
4343

4444
// Iteration #3.1 Bonus:
45-
function sum(arrOfNumbers) {
46-
let sum = 0;
47-
let sumOfString = '';
48-
for (let i = 0; i < arrOfNumbers.length; i++) {
49-
sum += arrOfNumbers[i];
45+
function sum(arrMixed) {
46+
if (!arrMixed.length) return 0;
47+
48+
let sumMixed = 0;
49+
50+
// for of loop iterates over the array
51+
// conditional statements delare what to do with different datatypes:
52+
// if it is a string look at the length of it and add it to sum
53+
// if it is a boolean ask if it is true or false. add 1 for true
54+
// and 0 for false (? 1 : 0 could also be 10 : 20)
55+
56+
for (const item of arrMixed) {
57+
if (typeof item === 'string') {
58+
sumMixed += item.length;
59+
} else if (typeof item === 'boolean') {
60+
sumMixed += item ? 1 : 0;
61+
} else if (typeof item === 'number') {
62+
sumMixed += item;
63+
} else {
64+
throw new Error("Unsupported data type sir or ma'am");
65+
}
5066
}
51-
return sum;
67+
return sumMixed;
5268
}
53-
5469
// Iteration #4: Calculate the average
5570
// Level 1: Array of numbers
5671
const numbersAvg = [2, 6, 9, 10, 7, 4, 1, 9];
@@ -89,7 +104,31 @@ function averageWordLength(arr) {
89104
}
90105

91106
// Bonus - Iteration #4.1
92-
function avg() {}
107+
function avg(mixedArr) {
108+
if (!mixedArr.length) return null;
109+
110+
let sumMixed = 0;
111+
112+
// for of loop iterates over the array
113+
// conditional statements delare what to do with different datatypes:
114+
// if it is a string look at the length of it and add it to sum
115+
// if it is a boolean ask if it is true or false. add 1 for true
116+
// and 0 for false (? 1 : 0 could also be 10 : 20)
117+
118+
for (const item of mixedArr) {
119+
if (typeof item === 'string') {
120+
sumMixed += item.length;
121+
} else if (typeof item === 'boolean') {
122+
sumMixed += item ? 1 : 0;
123+
} else if (typeof item === 'number') {
124+
sumMixed += item;
125+
} else {
126+
throw new Error("Unsupported data type sir or ma'am");
127+
}
128+
}
129+
let toManyDigits = sumMixed / mixedArr.length;
130+
return +toManyDigits.toFixed(2);
131+
}
93132

94133
// Iteration #5: Unique arrays
95134
const wordsUnique = [

0 commit comments

Comments
 (0)