Skip to content

Commit 20da5bc

Browse files
author
Javi Sastre
committed
semi finished exercise
1 parent bbd5108 commit 20da5bc

File tree

1 file changed

+120
-22
lines changed

1 file changed

+120
-22
lines changed

src/functions-and-arrays.js

Lines changed: 120 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,116 @@
11
// Iteration #1: Find the maximum
2-
function maxOfTwoNumbers() {}
3-
4-
2+
function maxOfTwoNumbers(num1, num2) {
3+
// if (num1 > num2) {
4+
// return num1;
5+
// } else if ( num1 < num2) {
6+
// return num2;
7+
// } else {
8+
// return num1;
9+
// }
10+
return Math.max(num1, num2);
11+
}
512

613
// Iteration #2: Find longest word
714
const words = ['mystery', 'brother', 'aviator', 'crocodile', 'pearl', 'orchard', 'crackpot'];
815

9-
function findLongestWord() {}
16+
function findLongestWord(words) {
17+
if (words.length === 0) return null;
1018

19+
let longestWord = '';
1120

21+
for (let i = 0; i < words.length; i++) {
22+
if (longestWord.length < words[i].length) {
23+
longestWord = words[i];
24+
}
25+
}
26+
return longestWord;
27+
}
1228

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

16-
function sumNumbers() {}
17-
18-
32+
function sumNumbers(numbers) {
33+
if (numbers.length === 0) return 0;
34+
let result = 0;
35+
for (let i = 0; i < numbers.length; i++) {
36+
result += numbers[i];
37+
}
38+
return result;
39+
}
1940

2041
// Iteration #3.1 Bonus:
21-
function sum() {}
22-
23-
42+
const mixedArr = [6, 12, 'miami', 1, true, 'barca', '200', 'lisboa', 8, 10];
43+
44+
function sum(mixedArr) {
45+
if (mixedArr.length === 0) return 0;
46+
47+
let result = 0;
48+
for (let i = 0; i < mixedArr.length; i++) {
49+
const element = mixedArr[i];
50+
51+
if (typeof element === 'string') {
52+
result += element.length;
53+
} else if (element === true) {
54+
result += 1;
55+
} else if (typeof element === 'object') {
56+
throw new Error("Unsupported data type sir or ma'am");
57+
} else {
58+
result += mixedArr[i];
59+
}
60+
}
61+
return result;
62+
}
2463

2564
// Iteration #4: Calculate the average
2665
// Level 1: Array of numbers
2766
const numbersAvg = [2, 6, 9, 10, 7, 4, 1, 9];
2867

29-
function averageNumbers() {}
68+
function averageNumbers(numbers) {
69+
if (numbers.length === 0) return null;
70+
if (numbers.length === 1) return numbers[0];
3071

72+
let total = 0;
73+
for (let k = 0; k < numbers.length; k++) {
74+
total += numbers[k];
75+
}
76+
return total / numbers.length;
77+
}
3178

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

35-
function averageWordLength() { }
82+
function averageWordLength(wordsArray) {
83+
if (wordsArray.length === 0) return null;
84+
let total = 0;
85+
86+
for (let j = 0; j < wordsArray.length; j++) {
87+
total += wordsArray[j].length;
88+
}
89+
90+
return total / wordsArray.length;
91+
}
3692

3793
// Bonus - Iteration #4.1
38-
function avg() {}
94+
const mixedDataArr = [6, 12, 'miami', 1, true, 'barca', '200', 'lisboa', 8, 10];
95+
96+
function avg(mixedDataArr) {
97+
if (mixedDataArr.length === 0) return null;
98+
99+
let total = 0;
100+
101+
for (let l = 0; l < mixedDataArr.length; l++) {
102+
if (typeof mixedDataArr[l] === 'string') {
103+
total += mixedDataArr[l].length;
104+
} else if (typeof mixedDataArr[l] === 'boolean') {
105+
if (mixedDataArr === true) total += 1;
106+
} else {
107+
total += mixedDataArr[l];
108+
}
109+
}
110+
let avg = (total / mixedDataArr.length).toFixed(2);
111+
112+
return Number(avg);
113+
}
39114

40115
// Iteration #5: Unique arrays
41116
const wordsUnique = [
@@ -52,16 +127,30 @@ const wordsUnique = [
52127
'bring'
53128
];
54129

55-
function uniquifyArray() {}
56-
130+
function uniquifyArray(wordsUni) {
131+
if (wordsUni.length === 0) return null;
57132

133+
let cleanWordsUni = [];
134+
wordsUni.forEach((element) => {
135+
if (!cleanWordsUni.includes(element)) {
136+
cleanWordsUni.push(element);
137+
}
138+
});
139+
return cleanWordsUni;
140+
}
58141

59142
// Iteration #6: Find elements
60143
const wordsFind = ['machine', 'subset', 'trouble', 'starting', 'matter', 'eating', 'truth', 'disobedience'];
61144

62-
function doesWordExist() {}
63-
145+
function doesWordExist(arraySearch, wordSearch) {
146+
if (arraySearch.length === 0) return null;
64147

148+
if (arraySearch.includes(wordSearch)) {
149+
return true;
150+
} else {
151+
return false;
152+
}
153+
}
65154

66155
// Iteration #7: Count repetition
67156
const wordsCount = [
@@ -78,9 +167,18 @@ const wordsCount = [
78167
'matter'
79168
];
80169

81-
function howManyTimes() {}
170+
function howManyTimes(arrayToCount, wordToCount) {
171+
let count = 0;
172+
if (arrayToCount.length === 0) return 0;
82173

174+
arrayToCount.forEach((element) => {
175+
if (element === wordToCount) {
176+
count++;
177+
}
178+
});
83179

180+
return count;
181+
}
84182

85183
// Iteration #8: Bonus
86184
const matrix = [
@@ -106,10 +204,10 @@ const matrix = [
106204
[1, 70, 54, 71, 83, 51, 54, 69, 16, 92, 33, 48, 61, 43, 52, 1, 89, 19, 67, 48]
107205
];
108206

109-
function greatestProduct() {}
110-
111-
112-
207+
function greatestProduct(matrix) {
208+
let biggest = 0;
209+
210+
}
113211

114212
// The following is required to make unit tests work.
115213
/* Environment setup. Do not modify the below code. */

0 commit comments

Comments
 (0)