|
1 | 1 | // Iteration #1: Find the maximum |
| 2 | +function maxOfTwoNumbers(num1, num2) { |
| 3 | + if (typeof num1 == "number" && typeof num2 == "number") { |
| 4 | + if (num1 > num2) { |
| 5 | + return num1; |
| 6 | + } else if (num2 > num1) { |
| 7 | + return num2; |
| 8 | + } else if (num1 === num2) { |
| 9 | + return num1; |
| 10 | + } else { |
| 11 | + console.log("This is not a valid operation!"); |
| 12 | + } |
| 13 | + } else { |
| 14 | + console.log("This is not a valid type of input"); |
| 15 | + }; |
| 16 | +}; |
2 | 17 |
|
3 | 18 | // Iteration #2: Find longest word |
4 | | -const words = ['mystery', 'brother', 'aviator', 'crocodile', 'pearl', 'orchard', 'crackpot']; |
| 19 | +function findLongestWord(arr) { |
| 20 | + let longestWord = ""; |
| 21 | + if (arr.length === 0) { |
| 22 | + return null; |
| 23 | + } else if (arr.length > 0) { |
| 24 | + for (let i = 0; i < arr.length; i++) { |
| 25 | + if (longestWord.length < arr[i].length) { |
| 26 | + longestWord = arr[i]; |
| 27 | + } |
| 28 | + } |
| 29 | + return longestWord; |
| 30 | + } |
| 31 | +}; |
5 | 32 |
|
6 | 33 | // Iteration #3: Calculate the sum |
7 | | - |
8 | 34 | const numbers = [6, 12, 1, 18, 13, 16, 2, 1, 8, 10]; |
9 | 35 |
|
| 36 | +function sumArray(arr) { |
| 37 | + let newArraySum = 0; |
| 38 | + for (let i = 0; i < arr.length; i++) { |
| 39 | + newArraySum += arr[i]; |
| 40 | + } |
| 41 | + return newArraySum; |
| 42 | +}; |
| 43 | + |
10 | 44 | // Iteration #4: Calculate the average |
11 | 45 | // Level 1: Array of numbers |
12 | 46 | const numbersAvg = [2, 6, 9, 10, 7, 4, 1, 9]; |
13 | 47 |
|
| 48 | +function averageNumbers(arr) { |
| 49 | + let sumArr = 0; |
| 50 | + if (arr.length === 0) { |
| 51 | + return null; |
| 52 | + } |
| 53 | + for (let i = 0; i < arr.length; i++) { |
| 54 | + sumArr += arr[i]; |
| 55 | + } |
| 56 | + let arrayAvg = sumArr / arr.length; |
| 57 | + return arrayAvg; |
| 58 | +}; |
| 59 | + |
14 | 60 | // Level 2: Array of strings |
15 | 61 | const wordsArr = [ |
16 | 62 | 'seat', |
|
0 commit comments