Skip to content

Commit 5f30d25

Browse files
committed
Iteration ironhack-labs#3.1 Completed
1 parent 4cb03c7 commit 5f30d25

File tree

2 files changed

+89
-9
lines changed

2 files changed

+89
-9
lines changed

src/functions-and-arrays.js

Lines changed: 88 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,105 @@
11
// Iteration #1: Find the maximum
2-
function maxOfTwoNumbers() {}
3-
2+
function maxOfTwoNumbers(number1,number2) {
3+
if (number1 > number2) {
4+
return number1
5+
} else if (number1 < number2) {
6+
return number2
7+
}
8+
return number2
9+
}
410

511

612
// Iteration #2: Find longest word
713
const words = ['mystery', 'brother', 'aviator', 'crocodile', 'pearl', 'orchard', 'crackpot'];
814

9-
function findLongestWord() {}
10-
11-
15+
function findLongestWord(words) {
16+
// Checking how big is the array.
17+
const wordsLength = words.length;
18+
19+
if (Array.isArray(words) === true) {
20+
if (wordsLength === 0) return null;
21+
if (wordsLength === 1) return words[0];
22+
23+
// Start with a string and set to 0
24+
let longestString = "";
25+
26+
// Loop throug the array and check if the current longestString is smaller than
27+
//current world for litterration.
28+
29+
if (wordsLength > 1) {
30+
for (let i = 0; i < wordsLength; i++) {
31+
if (words[i].length > longestString.length) {
32+
longestString = words[i];
33+
}
34+
}
35+
return longestString;
36+
}
37+
}
38+
return "We expected a array";
39+
}
1240

1341
// Iteration #3: Calculate the sum
1442
const numbers = [6, 12, 1, 18, 13, 16, 2, 1, 8, 10];
1543

16-
function sumNumbers() {}
44+
function sumNumbers(numbers) {
1745

46+
// Checking how big is the array.
47+
const numbersLength = numbers.length;
48+
49+
if (Array.isArray(numbers) === true) {
50+
if (numbersLength === 0) return 0;
51+
if (numbersLength === 1) return numbers[0];
52+
53+
let sumAddition = 0;
54+
for(let i = 0; i < numbersLength; i++){
55+
sumAddition += numbers[i];
56+
}
57+
return sumAddition;
58+
}
59+
return "We expected a array";
60+
}
1861

1962

20-
// Iteration #3.1 Bonus:
21-
function sum() {}
2263

64+
// Iteration #3.1 Bonus:
65+
// const mixedArr = [6, 12, 'miami', 1, true, 'barca', '200', 'lisboa', 8, 10];
66+
67+
function sum(numbers) {
68+
// Checking how big is the array.
69+
const numbersLength = numbers.length;
70+
71+
if (Array.isArray(numbers) === true) {
72+
if (numbersLength === 0) return 0;
73+
if (numbersLength === 1) return numbers[0];
74+
75+
let sumAddition = 0;
76+
77+
for (let i = 0; i < numbersLength; i++) {
78+
if (typeof numbers[i] === "object") {
79+
throw new Error("Unsupported data type sir or a'am");
80+
} else {
81+
if (typeof numbers[i] === "number") {
82+
sumAddition += numbers[i];
83+
} else if (typeof numbers[i] === "string") {
84+
let holdNumberLenghtString = numbers[i].length;
85+
sumAddition += holdNumberLenghtString;
86+
} else if (typeof numbers[i] === "boolean") {
87+
// Checking if Boolean are True or False
88+
if (numbers[i] === false) {
89+
let booleanAdd = 0;
90+
sumAddition += booleanAdd;
91+
} else if (numbers[i] === true) {
92+
let booleanAdd = 1;
93+
sumAddition += booleanAdd;
94+
}
95+
}
96+
}
97+
}
98+
99+
return sumAddition;
100+
}
101+
return "We expected a array";
102+
}
23103

24104

25105
// Iteration #4: Calculate the average

tests/functions-and-arrays.spec.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ describe('Bonus: Calculate the sum', () => {
121121

122122
it('should throw an error when unsupported data type (object or array) present in the array', () => {
123123
expect(() => sum([6, 12, 'miami', 1, 'barca', '200', 'lisboa', 8, [], {}])).toThrow(
124-
new Error("Unsupported data type sir or ma'am")
124+
new Error("Unsupported data type sir or a'am")
125125
);
126126
});
127127

0 commit comments

Comments
 (0)