Skip to content

Commit bc5b6cd

Browse files
committed
build: finish all exercise except the bonus ironhack-labs#8
1 parent 4cb03c7 commit bc5b6cd

File tree

1 file changed

+70
-55
lines changed

1 file changed

+70
-55
lines changed

src/functions-and-arrays.js

Lines changed: 70 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,86 +1,104 @@
11
// Iteration #1: Find the maximum
2-
function maxOfTwoNumbers() {}
3-
4-
2+
function maxOfTwoNumbers(num1, num2) {
3+
return num1 > num2 ? num1 : num2;
4+
}
55

66
// Iteration #2: Find longest word
7-
const words = ['mystery', 'brother', 'aviator', 'crocodile', 'pearl', 'orchard', 'crackpot'];
8-
9-
function findLongestWord() {}
10-
7+
const words = ["mystery", "brother", "aviator", "crocodile", "pearl", "orchard", "crackpot"];
118

9+
function findLongestWord(words) {
10+
return words.length === 0 ? null : words.reduce((acc, cur) => (cur.length > acc.length ? cur : acc), "");
11+
}
1212

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

16-
function sumNumbers() {}
17-
18-
16+
function sumNumbers(numbers) {
17+
return numbers.reduce((acc, cur) => (acc += cur), 0);
18+
}
1919

2020
// Iteration #3.1 Bonus:
21-
function sum() {}
22-
23-
21+
function sum(mixedArr) {
22+
return mixedArr.reduce((acc, cur) => {
23+
if (typeof cur === "number" || typeof cur === "boolean") {
24+
return (acc += cur);
25+
} else if (typeof cur === "string") {
26+
return (acc += cur.length);
27+
} else {
28+
throw new Error("unsupported data type (object or array) present in the array");
29+
}
30+
}, 0);
31+
}
2432

2533
// Iteration #4: Calculate the average
2634
// Level 1: Array of numbers
2735
const numbersAvg = [2, 6, 9, 10, 7, 4, 1, 9];
2836

29-
function averageNumbers() {}
30-
37+
function averageNumbers(numbersAvg) {
38+
return numbersAvg.length === 0 ? null : sumNumbers(numbersAvg) / numbersAvg.length;
39+
}
3140

3241
// Level 2: Array of strings
33-
const wordsArr = ['seat', 'correspond', 'linen', 'motif', 'hole', 'smell', 'smart', 'chaos', 'fuel', 'palace'];
42+
const wordsArr = ["seat", "correspond", "linen", "motif", "hole", "smell", "smart", "chaos", "fuel", "palace"];
3443

35-
function averageWordLength() { }
44+
function averageWordLength(wordsArr) {
45+
const numArr = wordsArr.map((word) => word.length);
46+
return averageNumbers(numArr);
47+
}
3648

3749
// Bonus - Iteration #4.1
38-
function avg() {}
50+
function avg(mixedArr) {
51+
return mixedArr.length === 0 ? null : sum(mixedArr) / mixedArr.length;
52+
}
3953

4054
// Iteration #5: Unique arrays
4155
const wordsUnique = [
42-
'crab',
43-
'poison',
44-
'contagious',
45-
'simple',
46-
'bring',
47-
'sharp',
48-
'playground',
49-
'poison',
50-
'communion',
51-
'simple',
52-
'bring'
56+
"crab",
57+
"poison",
58+
"contagious",
59+
"simple",
60+
"bring",
61+
"sharp",
62+
"playground",
63+
"poison",
64+
"communion",
65+
"simple",
66+
"bring",
5367
];
5468

55-
function uniquifyArray() {}
56-
69+
function uniquifyArray(wordsUnique) {
70+
return wordsUnique.length === 0 ? null : wordsUnique.filter((word, i, arr) => arr.indexOf(word) == i);
71+
}
5772

73+
// function uniquifyArray(wordsUnique) {
74+
// return [...new Set(wordsUnique)]
75+
// }
5876

5977
// Iteration #6: Find elements
60-
const wordsFind = ['machine', 'subset', 'trouble', 'starting', 'matter', 'eating', 'truth', 'disobedience'];
61-
62-
function doesWordExist() {}
63-
78+
const wordsFind = ["machine", "subset", "trouble", "starting", "matter", "eating", "truth", "disobedience"];
6479

80+
function doesWordExist(wordsFind, word) {
81+
return wordsFind.length === 0 ? null : wordsFind.includes(word);
82+
}
6583

6684
// Iteration #7: Count repetition
6785
const wordsCount = [
68-
'machine',
69-
'matter',
70-
'subset',
71-
'trouble',
72-
'starting',
73-
'matter',
74-
'eating',
75-
'matter',
76-
'truth',
77-
'disobedience',
78-
'matter'
86+
"machine",
87+
"matter",
88+
"subset",
89+
"trouble",
90+
"starting",
91+
"matter",
92+
"eating",
93+
"matter",
94+
"truth",
95+
"disobedience",
96+
"matter",
7997
];
8098

81-
function howManyTimes() {}
82-
83-
99+
function howManyTimes(wordsCount, word) {
100+
return wordsCount.reduce((acc, cur) => (cur === word ? (acc += 1) : acc), 0);
101+
}
84102

85103
// Iteration #8: Bonus
86104
const matrix = [
@@ -103,17 +121,14 @@ const matrix = [
103121
[4, 42, 16, 73, 38, 25, 39, 11, 24, 94, 72, 18, 8, 46, 29, 32, 40, 62, 76, 36],
104122
[20, 69, 36, 41, 72, 30, 23, 88, 34, 62, 99, 69, 82, 67, 59, 85, 74, 4, 36, 16],
105123
[20, 73, 35, 29, 78, 31, 90, 1, 74, 31, 49, 71, 48, 86, 81, 16, 23, 57, 5, 54],
106-
[1, 70, 54, 71, 83, 51, 54, 69, 16, 92, 33, 48, 61, 43, 52, 1, 89, 19, 67, 48]
124+
[1, 70, 54, 71, 83, 51, 54, 69, 16, 92, 33, 48, 61, 43, 52, 1, 89, 19, 67, 48],
107125
];
108126

109127
function greatestProduct() {}
110128

111-
112-
113-
114129
// The following is required to make unit tests work.
115130
/* Environment setup. Do not modify the below code. */
116-
if (typeof module !== 'undefined') {
131+
if (typeof module !== "undefined") {
117132
module.exports = {
118133
maxOfTwoNumbers,
119134
findLongestWord,
@@ -125,6 +140,6 @@ if (typeof module !== 'undefined') {
125140
uniquifyArray,
126141
doesWordExist,
127142
howManyTimes,
128-
greatestProduct
143+
greatestProduct,
129144
};
130145
}

0 commit comments

Comments
 (0)