Skip to content

Commit dbc99ee

Browse files
committed
W6-D1-LAB1
1 parent 8ff766d commit dbc99ee

File tree

1 file changed

+172
-13
lines changed

1 file changed

+172
-13
lines changed

src/functions-and-arrays.js

+172-13
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,118 @@
11
// Iteration #1: Find the maximum
2-
function maxOfTwoNumbers() {}
3-
4-
2+
function maxOfTwoNumbers(x,y) {
3+
if (x > y) {
4+
return x;
5+
}
6+
else {
7+
return y;
8+
}
9+
}
510

611
// Iteration #2: Find longest word
712
const words = ['mystery', 'brother', 'aviator', 'crocodile', 'pearl', 'orchard', 'crackpot'];
813

9-
function findLongestWord() {}
14+
function findLongestWord(wordsArray) {
15+
if (wordsArray.length === 0) return null;
16+
17+
let longestWord = wordsArray[0];
1018

19+
for (let i = 1; i < wordsArray.length; i++) {
20+
if (wordsArray[i].length > longestWord.length) {
21+
longestWord = wordsArray[i];
22+
}
23+
}
24+
25+
return longestWord;
26+
}
27+
28+
console.log(findLongestWord(words));
1129

1230

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

16-
function sumNumbers() {}
34+
function sumNumbers(numbersArray) {
35+
let sum = 0;
36+
37+
for (let i = 0; i < numbersArray.length; i++) {
38+
sum += numbersArray[i];
39+
}
1740

41+
return sum;
42+
}
1843

1944

2045
// Iteration #3.1 Bonus:
21-
function sum() {}
46+
function sum(numArr) {
47+
if (numArr.length === 0) return 0;
48+
49+
let totalSumArr = 0;
50+
51+
for (let i = 0; i < numArr.length; i++) {
52+
if (typeof numArr[i] === "number") {
53+
totalSumArr += numArr[i];
54+
}
55+
else if (typeof numArr[i] === "string") {
56+
totalSumArr += numArr[i].length;
57+
}
58+
else if (typeof numArr[i] === "boolean") {
59+
totalSumArr += numArr[i] ? 1 : 0;
60+
}
61+
else {
62+
throw new Error("Unsupported data type");
63+
}
64+
}
65+
66+
return totalSumArr;
67+
}
2268

2369

2470

2571
// Iteration #4: Calculate the average
2672
// Level 1: Array of numbers
2773
const numbersAvg = [2, 6, 9, 10, 7, 4, 1, 9];
2874

29-
function averageNumbers() {}
75+
function averageNumbers(numbersArray) {
76+
if (numbersArray.length === 0) return null;
77+
78+
let arraySum = sumNumbers(numbersArray);
79+
return arraySum / numbersArray.length;
80+
}
3081

3182

3283
// Level 2: Array of strings
3384
const wordsArr = ['seat', 'correspond', 'linen', 'motif', 'hole', 'smell', 'smart', 'chaos', 'fuel', 'palace'];
3485

35-
function averageWordLength() { }
86+
function averageWordLength(stringArray) {
87+
if (stringArray.length === 0) return null;
88+
89+
let totalLength = 0;
90+
91+
for (let i = 0; i < stringArray.length; i++) {
92+
totalLength += stringArray[i].length;
93+
}
94+
95+
return totalLength / stringArray.length;
96+
}
3697

3798
// Bonus - Iteration #4.1
38-
function avg() {}
99+
function avg(arr) {
100+
if (arr.length === 0) return null;
101+
102+
let totalSum = 0;
103+
104+
for (let i = 0; i < arr.length; i++) {
105+
if (typeof arr[i] === "number") {
106+
totalSum += arr[i];
107+
} else if (typeof arr[i] === "string") {
108+
totalSum += arr[i].length;
109+
} else if (typeof arr[i] === "boolean") {
110+
totalSum += arr[i] ? 1 : 0;
111+
}
112+
}
113+
114+
return totalSum / arr.length;
115+
}
39116

40117
// Iteration #5: Unique arrays
41118
const wordsUnique = [
@@ -52,14 +129,36 @@ const wordsUnique = [
52129
'bring'
53130
];
54131

55-
function uniquifyArray() {}
132+
function uniquifyArray(words) {
133+
if (words.length === 0) return null;
134+
135+
let result = [];
136+
137+
for (let i = 0; i < words.length; i++) {
138+
if (result.indexOf(words[i]) === -1) {
139+
result.push(words[i]);
140+
}
141+
}
142+
143+
return result;
144+
}
56145

57146

58147

59148
// Iteration #6: Find elements
60149
const wordsFind = ['machine', 'subset', 'trouble', 'starting', 'matter', 'eating', 'truth', 'disobedience'];
61150

62-
function doesWordExist() {}
151+
function doesWordExist(wordsArr, wordToFind) {
152+
if (wordsArr.length === 0) return null;
153+
154+
for (let i = 0; i < wordsArr.length; i++) {
155+
if (wordsArr[i] === wordToFind) {
156+
return true;
157+
}
158+
}
159+
160+
return false;
161+
}
63162

64163

65164

@@ -78,7 +177,19 @@ const wordsCount = [
78177
'matter'
79178
];
80179

81-
function howManyTimes() {}
180+
function howManyTimes(ArrayWords, wordToCount) {
181+
if (ArrayWords.length === 0) return 0;
182+
183+
let count = 0;
184+
185+
for (let i = 0; i < ArrayWords.length; i++) {
186+
if (ArrayWords[i] === wordToCount) {
187+
count++;
188+
}
189+
}
190+
191+
return count;
192+
}
82193

83194

84195

@@ -106,7 +217,55 @@ const matrix = [
106217
[1, 70, 54, 71, 83, 51, 54, 69, 16, 92, 33, 48, 61, 43, 52, 1, 89, 19, 67, 48]
107218
];
108219

109-
function greatestProduct() {}
220+
//8.1 Product of adjacent numbers (horizontally or vertically)
221+
222+
function greatestProduct(matrix) {
223+
let maxProduct = 0;
224+
225+
for (let row = 0; row < matrix.length; row++) {
226+
for (let col = 0; col < matrix[row].length; col++) {
227+
228+
// Horizontal
229+
if (col + 3 < matrix[row].length) {
230+
let product = matrix[row][col] * matrix[row][col + 1] * matrix[row][col + 2] * matrix[row][col + 3];
231+
maxProduct = Math.max(maxProduct, product);
232+
}
233+
234+
// Vertical
235+
if (row + 3 < matrix.length) {
236+
let product = matrix[row][col] * matrix[row + 1][col] * matrix[row + 2][col] * matrix[row + 3][col];
237+
maxProduct = Math.max(maxProduct, product);
238+
}
239+
}
240+
}
241+
242+
return maxProduct;
243+
}
244+
245+
//8.2 Product of diagonals
246+
247+
function greatestProductOfDiagonals(matrix) {
248+
let maxProduct = 0;
249+
250+
for (let row = 0; row < matrix.length; row++) {
251+
for (let col = 0; col < matrix[row].length; col++) {
252+
253+
// Diagonal ↘
254+
if (row + 3 < matrix.length && col + 3 < matrix[row].length) {
255+
let product = matrix[row][col] * matrix[row + 1][col + 1] * matrix[row + 2][col + 2] * matrix[row + 3][col + 3];
256+
maxProduct = Math.max(maxProduct, product);
257+
}
258+
259+
// Diagonal ↙
260+
if (row + 3 < matrix.length && col - 3 >= 0) {
261+
let product = matrix[row][col] * matrix[row + 1][col - 1] * matrix[row + 2][col - 2] * matrix[row + 3][col - 3];
262+
maxProduct = Math.max(maxProduct, product);
263+
}
264+
}
265+
}
266+
267+
return maxProduct;
268+
}
110269

111270

112271

0 commit comments

Comments
 (0)