|
1 | 1 | // 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 | + |
3 | 14 |
|
4 | 15 |
|
5 | 16 |
|
6 | 17 | // Iteration #2: Find longest word
|
7 | 18 | const words = ['mystery', 'brother', 'aviator', 'crocodile', 'pearl', 'orchard', 'crackpot'];
|
8 | 19 |
|
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)); |
10 | 24 |
|
11 | 25 |
|
12 | 26 |
|
13 | 27 | // Iteration #3: Calculate the sum
|
14 | 28 | const numbers = [6, 12, 1, 18, 13, 16, 2, 1, 8, 10];
|
15 | 29 |
|
16 |
| -function sumNumbers() {} |
| 30 | +sumNumbers = (numbers) => numbers.reduce(function(a, b){ |
| 31 | + return a + b; |
| 32 | + }, 0); |
17 | 33 |
|
| 34 | +console.log("the sum of numbers is " + sumNumbers(numbers)); |
18 | 35 |
|
19 | 36 |
|
20 | 37 | // 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 | +} |
24 | 68 |
|
25 | 69 | // Iteration #4: Calculate the average
|
26 | 70 | // Level 1: Array of numbers
|
27 | 71 | 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)) |
31 | 80 |
|
32 | 81 | // Level 2: Array of strings
|
33 | 82 | 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 | + |
34 | 95 |
|
35 |
| -function averageWordLength() { } |
36 | 96 |
|
37 | 97 | // Bonus - Iteration #4.1
|
38 |
| -function avg() {} |
| 98 | +// should return: 5.7 |
| 99 | +function avg(arr){} |
39 | 100 |
|
40 | 101 | // Iteration #5: Unique arrays
|
41 | 102 | const wordsUnique = [
|
@@ -125,6 +186,6 @@ if (typeof module !== 'undefined') {
|
125 | 186 | uniquifyArray,
|
126 | 187 | doesWordExist,
|
127 | 188 | howManyTimes,
|
128 |
| - greatestProduct |
| 189 | + greatestProduct, |
129 | 190 | };
|
130 | 191 | }
|
0 commit comments