|
1 | 1 | // Iteration #1: Find the maximum
|
2 |
| -function maxOfTwoNumbers() {} |
| 2 | +function maxOfTwoNumbers(a,b) { |
| 3 | + if(a>b) { |
| 4 | + return a |
| 5 | + } else { |
| 6 | + return b |
| 7 | + } |
| 8 | +} |
3 | 9 |
|
4 | 10 |
|
5 | 11 |
|
6 | 12 | // Iteration #2: Find longest word
|
7 |
| -const words = ['mystery', 'brother', 'aviator', 'crocodile', 'pearl', 'orchard', 'crackpot']; |
| 13 | +const words = ['mystery', 'brother', 'aviator', 'crocodile', 'pearl', 'orchard', 'crackpots']; |
| 14 | + |
| 15 | +function findLongestWord(arr) { |
| 16 | + let longestWordArr = []; |
| 17 | + if (arr.length === 0){ |
| 18 | + return null; |
| 19 | + } |
| 20 | + |
| 21 | + if (arr.length === 1){ |
| 22 | + return arr[0]; |
| 23 | + } |
| 24 | + |
| 25 | + let longestWord; |
| 26 | + for (i=0; i<(arr.length -1); i++){ |
| 27 | + if (arr[i].length >= arr[i+1].length){ |
| 28 | + longestWord=arr[i]; |
| 29 | + } |
| 30 | + } |
| 31 | + |
| 32 | + return longestWord; |
| 33 | + /* |
| 34 | + for (j=0; j<arr.length; j++){ |
| 35 | + if (arr[j].length === longestWord.length) { |
| 36 | + longestWordArr.push(arr[j]); |
| 37 | + } |
| 38 | + } |
| 39 | + |
| 40 | + return longestWordArr;*/ |
| 41 | +} |
8 | 42 |
|
9 |
| -function findLongestWord() {} |
| 43 | +//console.log(findLongestWord(words)); |
10 | 44 |
|
11 | 45 |
|
12 | 46 |
|
13 | 47 | // Iteration #3: Calculate the sum
|
14 | 48 | const numbers = [6, 12, 1, 18, 13, 16, 2, 1, 8, 10];
|
15 | 49 |
|
16 |
| -function sumNumbers() {} |
| 50 | +function sumNumbers(arr) { |
| 51 | + let sum=0; |
| 52 | + |
| 53 | + arr.forEach(number => { |
| 54 | + sum += number; |
| 55 | + }); |
| 56 | + return sum; |
| 57 | +} |
| 58 | + |
| 59 | +let result = sumNumbers(numbers); |
| 60 | +console.log(result); |
17 | 61 |
|
18 | 62 |
|
19 | 63 |
|
|
0 commit comments