Skip to content

Commit 449ea88

Browse files
committed
all
1 parent 8ff766d commit 449ea88

File tree

1 file changed

+98
-26
lines changed

1 file changed

+98
-26
lines changed

Diff for: src/functions-and-arrays.js

+98-26
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,94 @@
11
// Iteration #1: Find the maximum
2-
function maxOfTwoNumbers() {}
3-
4-
2+
function maxOfTwoNumbers(x, y) {
3+
if (x > y) return x;
4+
else return y;
5+
}
56

67
// Iteration #2: Find longest word
78
const words = ['mystery', 'brother', 'aviator', 'crocodile', 'pearl', 'orchard', 'crackpot'];
9+
function findLongestWord(words) {
810

9-
function findLongestWord() {}
10-
11-
11+
if (words.length === 0) return null;
12+
13+
return words.reduce((longest, current) =>
14+
current.length > longest.length ? current : longest
15+
);
16+
}
1217

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

16-
function sumNumbers() {}
21+
function sumNumbers(arr) {
1722

23+
if (arr.length === 0) {
24+
return 0;
25+
}
1826

27+
return arr.reduce((acc, curr) => {
1928

20-
// Iteration #3.1 Bonus:
21-
function sum() {}
29+
if (typeof curr === 'number') {
30+
return acc + curr;
31+
}
32+
return acc;
33+
}, 0);
2234

35+
}
36+
37+
// Iteration #3.1 Bonus:
38+
function sum(arr) {
39+
if (arr.length === 0) { return 0;
40+
}
2341

42+
return arr.reduce((acc, curr) => {
43+
if (typeof curr === 'number') {
44+
return acc + curr;
45+
}
46+
if (typeof curr === 'string') {
47+
return acc + curr.length;
48+
}
49+
if (typeof curr === 'boolean') {
50+
return acc + (curr ? 1 : 0);
51+
}
52+
53+
throw new Error('Unsupported data type');
54+
}, 0);
55+
}
2456

2557
// Iteration #4: Calculate the average
2658
// Level 1: Array of numbers
2759
const numbersAvg = [2, 6, 9, 10, 7, 4, 1, 9];
2860

29-
function averageNumbers() {}
30-
61+
function averageNumbers(arr) {
62+
if (arr.length === 0) return null;
63+
const sum = arr.reduce((acc, curr) => acc + curr, 0);
64+
return sum / arr.length;
65+
}
3166

3267
// Level 2: Array of strings
3368
const wordsArr = ['seat', 'correspond', 'linen', 'motif', 'hole', 'smell', 'smart', 'chaos', 'fuel', 'palace'];
34-
35-
function averageWordLength() { }
69+
function averageWordLength(arr) {
70+
if (arr.length === 0) return null;
71+
const sumLengths = arr.reduce((acc, word) => acc + word.length, 0);
72+
return sumLengths / arr.length;
73+
}
3674

3775
// Bonus - Iteration #4.1
38-
function avg() {}
76+
function avg(arr) {
77+
if (arr.length === 0) return null;
78+
79+
const sum = arr.reduce((acc, curr) => {
80+
if (typeof curr === 'number') {
81+
return acc + curr;
82+
} else if (typeof curr === 'string') {
83+
return acc + curr.length;
84+
} else if (typeof curr === 'boolean') {
85+
return acc + (curr ? 1 : 0);
86+
}
87+
return acc;
88+
}, 0);
89+
90+
return sum / arr.length;
91+
}
3992

4093
// Iteration #5: Unique arrays
4194
const wordsUnique = [
@@ -52,16 +105,20 @@ const wordsUnique = [
52105
'bring'
53106
];
54107

55-
function uniquifyArray() {}
56-
57-
108+
function uniquifyArray(arr) {
109+
if (arr.length === 0) return null;
110+
111+
return [...new Set(arr)];
112+
}
58113

59114
// Iteration #6: Find elements
60115
const wordsFind = ['machine', 'subset', 'trouble', 'starting', 'matter', 'eating', 'truth', 'disobedience'];
61116

62-
function doesWordExist() {}
63-
64-
117+
function doesWordExist(words, word) {
118+
if (words.length === 0) return null;
119+
120+
return words.includes(word);
121+
}
65122

66123
// Iteration #7: Count repetition
67124
const wordsCount = [
@@ -78,8 +135,11 @@ const wordsCount = [
78135
'matter'
79136
];
80137

81-
function howManyTimes() {}
82-
138+
function howManyTimes(words, word) {
139+
if (words.length === 0) return 0;
140+
141+
return words.filter(w => w === word).length;
142+
}
83143

84144

85145
// Iteration #8: Bonus
@@ -105,10 +165,22 @@ const matrix = [
105165
[20, 73, 35, 29, 78, 31, 90, 1, 74, 31, 49, 71, 48, 86, 81, 16, 23, 57, 5, 54],
106166
[1, 70, 54, 71, 83, 51, 54, 69, 16, 92, 33, 48, 61, 43, 52, 1, 89, 19, 67, 48]
107167
];
108-
109-
function greatestProduct() {}
110-
111-
168+
function greatestProduct(matrix) {
169+
let maxProduct = 0;
170+
for (let i = 0; i < matrix.length; i++) {
171+
for (let j = 0; j < matrix[i].length; j++) {
172+
if (j < matrix[i].length - 3) {
173+
let horizontal = matrix[i][j] * matrix[i][j+1] * matrix[i][j+2] * matrix[i][j+3];
174+
maxProduct = Math.max(maxProduct, horizontal);
175+
}
176+
if (i < matrix.length - 3) {
177+
let vertical = matrix[i][j] * matrix[i+1][j] * matrix[i+2][j] * matrix[i+3][j];
178+
maxProduct = Math.max(maxProduct, vertical);
179+
}
180+
}
181+
return maxProduct || 1;
182+
}
183+
}
112184

113185

114186
// The following is required to make unit tests work.

0 commit comments

Comments
 (0)