diff --git a/src/functions-and-arrays.js b/src/functions-and-arrays.js index 3a7dbec..e2b3be4 100644 --- a/src/functions-and-arrays.js +++ b/src/functions-and-arrays.js @@ -1,24 +1,76 @@ // Iteration #1: Find the maximum -function maxOfTwoNumbers() {} +function maxOfTwoNumbers(num1, num2){ + if(num1 > num2){ + return num1; + } + else{ + return num2; + } +} + +console.log(maxOfTwoNumbers(2,6)) // Iteration #2: Find longest word const words = ['mystery', 'brother', 'aviator', 'crocodile', 'pearl', 'orchard', 'crackpot']; -function findLongestWord() {} +function findLongestWord(words){ + let longestword = words[0] + + if (words.length === 0) return null; + for (i=1; i longestword.length){ + longestword = words[i]; + } + } + return longestword; +} + +console.log(findLongestWord(words)) // Iteration #3: Calculate the sum const numbers = [6, 12, 1, 18, 13, 16, 2, 1, 8, 10]; -function sumNumbers() {} +function sumNumbers(numbers){ + let sum = 0 + if (numbers.length === 0) return 0; + for(i=0; i maxProduct) { + maxProduct = product; + } + } + } + + for (let r = 0; r < rows - 3; r++) { + for (let c = 0; c < cols; c++) { + let product = matrix[r][c] * matrix[r+1][c] * matrix[r+2][c] * matrix[r+3][c]; + if (product > maxProduct) { + maxProduct = product; + } + } + } + + return maxProduct; +} +console.log(greatestProduct(matrix)); // The following is required to make unit tests work.