Skip to content

Commit beeb86b

Browse files
committed
add iteration 4 level 1 and 2
1 parent 678bce4 commit beeb86b

File tree

1 file changed

+26
-35
lines changed

1 file changed

+26
-35
lines changed

src/functions-and-arrays.js

Lines changed: 26 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
11
// Iteration #1: Find the maximum
22
function maxOfTwoNumbers(num1, num2) {
3-
const isGreater = num1 > num2
3+
const isGreater = num1 > num2;
44
if (isGreater) {
5-
return num1
5+
return num1;
66
} else {
7-
return num2
7+
return num2;
88
}
99
}
1010

11-
12-
1311
// Iteration #2: Find longest word
1412
const words = ['mystery', 'brother', 'aviator', 'crocodile', 'pearl', 'orchard', 'crackpot'];
1513

@@ -18,9 +16,9 @@ function findLongestWord(words) {
1816
if (isEmptyArr) {
1917
return null;
2018
} else {
21-
let longestWord = ""
19+
let longestWord = '';
2220
for (const word in words) {
23-
const isLonger = words[word].length > longestWord.length
21+
const isLonger = words[word].length > longestWord.length;
2422
if (isLonger) {
2523
longestWord = words[word];
2624
}
@@ -29,8 +27,6 @@ function findLongestWord(words) {
2927
}
3028
}
3129

32-
33-
3430
// Iteration #3: Calculate the sum
3531
const numbers = [6, 12, 1, 18, 13, 16, 2, 1, 8, 10];
3632

@@ -42,22 +38,21 @@ function sumNumbers(numbers) {
4238
return sum;
4339
}
4440

45-
4641
const mixedArr = [6, 12, 'miami', 1, true, 'barca', '200', 'lisboa', 8, 10];
4742
// Iteration #3.1 Bonus:
4843
function sum(mixedArr) {
4944
let sum = 0;
5045
for (let num of mixedArr) {
51-
const isString = typeof num === 'string'
52-
const isNotSupported = typeof num === 'object'
46+
const isString = typeof num === 'string';
47+
const isNotSupported = typeof num === 'object';
5348
if (isNotSupported) {
54-
throw Error("Unsupported data type sir or ma'am")
49+
throw Error("Unsupported data type sir or ma'am");
5550
}
5651
if (isString) {
5752
sum += num.length;
5853
continue;
5954
}
60-
sum += num
55+
sum += num;
6156
}
6257
return sum;
6358
}
@@ -69,23 +64,28 @@ const numbersAvg = [2, 6, 9, 10, 7, 4, 1, 9];
6964
function averageNumbers(numbersAvg) {
7065
const isEmptyArr = numbersAvg.length === 0;
7166
if (isEmptyArr) {
72-
return null
67+
return null;
7368
} else {
74-
let sum = sumNumbers(numbersAvg)
75-
let avg = sum / numbersAvg.length
69+
let sum = sumNumbers(numbersAvg);
70+
let avg = sum / numbersAvg.length;
7671
return avg;
7772
}
78-
7973
}
8074

81-
8275
// Level 2: Array of strings
8376
const wordsArr = ['seat', 'correspond', 'linen', 'motif', 'hole', 'smell', 'smart', 'chaos', 'fuel', 'palace'];
8477

85-
function averageWordLength() { }
86-
78+
function averageWordLength(wordsArr) {
79+
const isEmptyArr = wordsArr.length === 0;
80+
if (isEmptyArr) {
81+
return null;
82+
} else {
83+
const result = sum(wordsArr);
84+
return result / wordsArr.length;
85+
}
86+
}
8787
// Bonus - Iteration #4.1
88-
function avg() { }
88+
function avg() {}
8989

9090
// Iteration #5: Unique arrays
9191
const wordsUnique = [
@@ -102,16 +102,12 @@ const wordsUnique = [
102102
'bring'
103103
];
104104

105-
function uniquifyArray() { }
106-
107-
105+
function uniquifyArray() {}
108106

109107
// Iteration #6: Find elements
110108
const wordsFind = ['machine', 'subset', 'trouble', 'starting', 'matter', 'eating', 'truth', 'disobedience'];
111109

112-
function doesWordExist() { }
113-
114-
110+
function doesWordExist() {}
115111

116112
// Iteration #7: Count repetition
117113
const wordsCount = [
@@ -128,9 +124,7 @@ const wordsCount = [
128124
'matter'
129125
];
130126

131-
function howManyTimes() { }
132-
133-
127+
function howManyTimes() {}
134128

135129
// Iteration #8: Bonus
136130
const matrix = [
@@ -156,10 +150,7 @@ const matrix = [
156150
[1, 70, 54, 71, 83, 51, 54, 69, 16, 92, 33, 48, 61, 43, 52, 1, 89, 19, 67, 48]
157151
];
158152

159-
function greatestProduct() { }
160-
161-
162-
153+
function greatestProduct() {}
163154

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

0 commit comments

Comments
 (0)