Skip to content

Commit 0ea4c7e

Browse files
committed
solved 4
1 parent bbd5108 commit 0ea4c7e

File tree

1 file changed

+73
-12
lines changed

1 file changed

+73
-12
lines changed

src/functions-and-arrays.js

Lines changed: 73 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,102 @@
11
// Iteration #1: Find the maximum
2-
function maxOfTwoNumbers() {}
2+
function maxOfTwoNumbers(a,b){
3+
if (a>b){
4+
return a;
5+
} else if(b<a) {
6+
return b;
7+
}
8+
else {
9+
return "equal"
10+
}
11+
}
12+
console.log("the max of two numbers is " + maxOfTwoNumbers(2,4));
13+
314

415

516

617
// Iteration #2: Find longest word
718
const words = ['mystery', 'brother', 'aviator', 'crocodile', 'pearl', 'orchard', 'crackpot'];
819

9-
function findLongestWord() {}
20+
findLongestWord = (words) => words.reduce((a, b) =>
21+
{if (b.length > a.length) return b; else return a});
22+
23+
console.log("the longest word is " + findLongestWord(words));
1024

1125

1226

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

16-
function sumNumbers() {}
30+
sumNumbers = (numbers) => numbers.reduce(function(a, b){
31+
return a + b;
32+
}, 0);
1733

34+
console.log("the sum of numbers is " + sumNumbers(numbers));
1835

1936

2037
// Iteration #3.1 Bonus:
21-
function sum() {}
22-
23-
38+
const mixedArr = [6, 12, 'miami', 1, true, 'barca', '200', 'lisboa', 8, 10];
39+
// should return: 57
40+
function sum(array) {
41+
if (typeof array === "object" || typeof array === "array") {
42+
let element;
43+
let sum = 0;
44+
45+
for (i = 0; i < array.length; i++) {
46+
element = array[i]
47+
switch (typeof element) {
48+
case "boolean":
49+
if (element === true) {
50+
sum += 1;
51+
}
52+
break;
53+
case "string":
54+
sum += array[i].length;
55+
break;
56+
case "number":
57+
sum += array[i];
58+
break;
59+
default:
60+
sum = null;
61+
}
62+
}
63+
return sum;
64+
} else {
65+
return Error;
66+
}
67+
}
2468

2569
// Iteration #4: Calculate the average
2670
// Level 1: Array of numbers
2771
const numbersAvg = [2, 6, 9, 10, 7, 4, 1, 9];
28-
29-
function averageNumbers() {}
30-
72+
// return 6
73+
let averageNumbers = numbersAvg => {
74+
if (numbersAvg.length === 0) {
75+
return null
76+
}
77+
return sumNumbers(numbersAvg)/numbersAvg.length
78+
}
79+
console.log("the avg number is " + averageNumbers(numbersAvg))
3180

3281
// Level 2: Array of strings
3382
const wordsArr = ['seat', 'correspond', 'linen', 'motif', 'hole', 'smell', 'smart', 'chaos', 'fuel', 'palace'];
83+
// returns 5.3
84+
let sumWords = (wordsArr) => wordsArr.reduce((a, b) =>
85+
{if (b.length > a.length) return b; else return a});
86+
87+
let averageWordLength = wordsArr => {
88+
if (wordsArr.length === 0) {
89+
return null
90+
}
91+
return sumWords(wordsArr)/wordsArr.length
92+
}
93+
console.log("the average length of the words is " + averageWordLength(wordsArr))
94+
3495

35-
function averageWordLength() { }
3696

3797
// Bonus - Iteration #4.1
38-
function avg() {}
98+
// should return: 5.7
99+
function avg(arr){}
39100

40101
// Iteration #5: Unique arrays
41102
const wordsUnique = [
@@ -125,6 +186,6 @@ if (typeof module !== 'undefined') {
125186
uniquifyArray,
126187
doesWordExist,
127188
howManyTimes,
128-
greatestProduct
189+
greatestProduct,
129190
};
130191
}

0 commit comments

Comments
 (0)