diff --git a/src/functions-and-arrays.js b/src/functions-and-arrays.js index 3a7dbec41..3791111d4 100644 --- a/src/functions-and-arrays.js +++ b/src/functions-and-arrays.js @@ -1,19 +1,38 @@ // Iteration #1: Find the maximum -function maxOfTwoNumbers() {} - +function maxOfTwoNumbers(num1, num2) { + return Math.max(num1, num2); +} // Iteration #2: Find longest word const words = ['mystery', 'brother', 'aviator', 'crocodile', 'pearl', 'orchard', 'crackpot']; -function findLongestWord() {} - +function findLongestWord(anyArray) { + if (anyArray.length === 0) { + return null; + } + + let longestWord = anyArray[0]; + for (let i = 1; i < anyArray.length; i++) { + if (anyArray[i].length > longestWord.length) { + longestWord = anyArray[i]; + } + } + return longestWord; +} // Iteration #3: Calculate the sum + const numbers = [6, 12, 1, 18, 13, 16, 2, 1, 8, 10]; -function sumNumbers() {} +function sumNumbers(sumArray) { + let sum = 0; + for (let i = 0; i < sumArray.length; i++) { + sum += sumArray[i]; + } + return sum; +} @@ -26,16 +45,37 @@ function sum() {} // Level 1: Array of numbers const numbersAvg = [2, 6, 9, 10, 7, 4, 1, 9]; -function averageNumbers() {} +function averageNumbers(arrayAvg) { + if (arrayAvg.length === 0) { + return null; + } + let sum = 0; + for (let i = 0; i < arrayAvg.length; i++) { + sum += arrayAvg[i]; + } + return sum / arrayAvg.length; +} // Level 2: Array of strings const wordsArr = ['seat', 'correspond', 'linen', 'motif', 'hole', 'smell', 'smart', 'chaos', 'fuel', 'palace']; -function averageWordLength() { } +function averageWordLength(wordAvg) { + if (wordAvg.length === 0) { + return null; + } + let sum = 0; + for (let i = 0; i < wordAvg.length; i++) { + sum += wordAvg[i].length; + } + return sum / wordAvg.length; +} // Bonus - Iteration #4.1 -function avg() {} + +function avg() { + +} // Iteration #5: Unique arrays const wordsUnique = [ @@ -52,14 +92,30 @@ const wordsUnique = [ 'bring' ]; -function uniquifyArray() {} +function uniquifyArray(words) { + if (words.length === 0) { + return null; + } + let deduplicatedArray = []; + for (let i = 0; i < words.length; i++) { + if (deduplicatedArray.indexOf(words[i]) === -1) { + deduplicatedArray.push(words[i]); + } + } + return deduplicatedArray; +} // Iteration #6: Find elements const wordsFind = ['machine', 'subset', 'trouble', 'starting', 'matter', 'eating', 'truth', 'disobedience']; -function doesWordExist() {} +function doesWordExist(wordArr, wordFind) { + if (wordArr === 0){ + return null; + } +return wordArr.included(wordFind); +} @@ -78,8 +134,16 @@ const wordsCount = [ 'matter' ]; -function howManyTimes() {} +function howManyTimes(inputArray, countWord) { + let sum = 0; + for (let i = 0; i < inputArray.length; i++) { + if (inputArray[i] === countWord) { + sum += 1; + } + } + return sum; +} // Iteration #8: Bonus @@ -106,7 +170,42 @@ const matrix = [ [1, 70, 54, 71, 83, 51, 54, 69, 16, 92, 33, 48, 61, 43, 52, 1, 89, 19, 67, 48] ]; -function greatestProduct() {} +function greatestProduct(bigArray) { + let maxProduct = 0; + + // Find the greatest product of four adjacent numbers horizontally + for (let i = 0; i < bigArray.length; i++) { + for (let j = 0; j < bigArray[i].length - 3; j++) { + let product = + bigArray[i][j] * + bigArray[i][j + 1] * + bigArray[i][j + 2] * + bigArray[i][j + 3]; + if (product > maxProduct) { + maxProduct = product; + } + } + } + + // Find the greatest product of four adjacent numbers vertically + for (let i = 0; i < bigArray.length - 3; i++) { + for (let j = 0; j < bigArray[i].length; j++) { + let product = + bigArray[i][j] * + bigArray[i + 1][j] * + bigArray[i + 2][j] * + bigArray[i + 3][j]; + if (product > maxProduct) { + maxProduct = product; + } + } + } + + return maxProduct; +} + +const result = greatestProduct(matrix); +console.log(result);