|
1 | 1 | // Iteration #1: Find the maximum
|
2 |
| -function maxOfTwoNumbers() {} |
3 |
| - |
| 2 | +function maxOfTwoNumbers(number1,number2) { |
| 3 | + if (number1 > number2) { |
| 4 | + return number1 |
| 5 | + } else if (number1 < number2) { |
| 6 | + return number2 |
| 7 | + } |
| 8 | + return number2 |
| 9 | +} |
4 | 10 |
|
5 | 11 |
|
6 | 12 | // Iteration #2: Find longest word
|
7 | 13 | const words = ['mystery', 'brother', 'aviator', 'crocodile', 'pearl', 'orchard', 'crackpot'];
|
8 | 14 |
|
9 |
| -function findLongestWord() {} |
10 |
| - |
11 |
| - |
| 15 | +function findLongestWord(words) { |
| 16 | + // Checking how big is the array. |
| 17 | + const wordsLength = words.length; |
| 18 | + |
| 19 | + if (Array.isArray(words) === true) { |
| 20 | + if (wordsLength === 0) return null; |
| 21 | + if (wordsLength === 1) return words[0]; |
| 22 | + |
| 23 | + // Start with a string and set to 0 |
| 24 | + let longestString = ""; |
| 25 | + |
| 26 | + // Loop throug the array and check if the current longestString is smaller than |
| 27 | + //current world for litterration. |
| 28 | + |
| 29 | + if (wordsLength > 1) { |
| 30 | + for (let i = 0; i < wordsLength; i++) { |
| 31 | + if (words[i].length > longestString.length) { |
| 32 | + longestString = words[i]; |
| 33 | + } |
| 34 | + } |
| 35 | + return longestString; |
| 36 | + } |
| 37 | + } |
| 38 | + return "We expected a array"; |
| 39 | +} |
12 | 40 |
|
13 | 41 | // Iteration #3: Calculate the sum
|
14 | 42 | const numbers = [6, 12, 1, 18, 13, 16, 2, 1, 8, 10];
|
15 | 43 |
|
16 |
| -function sumNumbers() {} |
| 44 | +function sumNumbers(numbers) { |
17 | 45 |
|
| 46 | + // Checking how big is the array. |
| 47 | + const numbersLength = numbers.length; |
| 48 | + |
| 49 | + if (Array.isArray(numbers) === true) { |
| 50 | + if (numbersLength === 0) return 0; |
| 51 | + if (numbersLength === 1) return numbers[0]; |
| 52 | + |
| 53 | + let sumAddition = 0; |
| 54 | + for(let i = 0; i < numbersLength; i++){ |
| 55 | + sumAddition += numbers[i]; |
| 56 | + } |
| 57 | + return sumAddition; |
| 58 | + } |
| 59 | + return "We expected a array"; |
| 60 | +} |
18 | 61 |
|
19 | 62 |
|
20 |
| -// Iteration #3.1 Bonus: |
21 |
| -function sum() {} |
22 | 63 |
|
| 64 | +// Iteration #3.1 Bonus: |
| 65 | +// const mixedArr = [6, 12, 'miami', 1, true, 'barca', '200', 'lisboa', 8, 10]; |
| 66 | + |
| 67 | +function sum(numbers) { |
| 68 | + // Checking how big is the array. |
| 69 | + const numbersLength = numbers.length; |
| 70 | + |
| 71 | + if (Array.isArray(numbers) === true) { |
| 72 | + if (numbersLength === 0) return 0; |
| 73 | + if (numbersLength === 1) return numbers[0]; |
| 74 | + |
| 75 | + let sumAddition = 0; |
| 76 | + |
| 77 | + for (let i = 0; i < numbersLength; i++) { |
| 78 | + if (typeof numbers[i] === "object") { |
| 79 | + throw new Error("Unsupported data type sir or a'am"); |
| 80 | + } else { |
| 81 | + if (typeof numbers[i] === "number") { |
| 82 | + sumAddition += numbers[i]; |
| 83 | + } else if (typeof numbers[i] === "string") { |
| 84 | + let holdNumberLenghtString = numbers[i].length; |
| 85 | + sumAddition += holdNumberLenghtString; |
| 86 | + } else if (typeof numbers[i] === "boolean") { |
| 87 | + // Checking if Boolean are True or False |
| 88 | + if (numbers[i] === false) { |
| 89 | + let booleanAdd = 0; |
| 90 | + sumAddition += booleanAdd; |
| 91 | + } else if (numbers[i] === true) { |
| 92 | + let booleanAdd = 1; |
| 93 | + sumAddition += booleanAdd; |
| 94 | + } |
| 95 | + } |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + return sumAddition; |
| 100 | + } |
| 101 | + return "We expected a array"; |
| 102 | +} |
23 | 103 |
|
24 | 104 |
|
25 | 105 | // Iteration #4: Calculate the average
|
|
0 commit comments