diff --git a/src/functions-and-arrays.js b/src/functions-and-arrays.js index 3a7dbec..79f50bb 100644 --- a/src/functions-and-arrays.js +++ b/src/functions-and-arrays.js @@ -1,24 +1,55 @@ // Iteration #1: Find the maximum -function maxOfTwoNumbers() {} +function maxOfTwoNumbers(a, b) { + return a > b ? a : b; +} // Iteration #2: Find longest word const words = ['mystery', 'brother', 'aviator', 'crocodile', 'pearl', 'orchard', 'crackpot']; -function findLongestWord() {} +function findLongestWord(words) { + if (words.length === 0) return null; + let longestWord = words[0]; + for (let word of words) { + if (word.length > longestWord.length) { + longestWord = word; + } + } + return longestWord; +} // 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; + for (let number of numbers) { + sum += number; + } + return sum; +} // Iteration #3.1 Bonus: -function sum() {} +function sum(arr) { + let total = 0; + for (let item of arr) { + if (typeof item === 'number') { + total += item; + } else if (typeof item === 'string') { + total += item.length; + } else if (typeof item === 'boolean') { + total += item ? 1 : 0; + } else { + throw new Error("Unsupported data type sir or ma'am"); + } + } + return total; +} @@ -26,16 +57,25 @@ function sum() {} // Level 1: Array of numbers const numbersAvg = [2, 6, 9, 10, 7, 4, 1, 9]; -function averageNumbers() {} - - +function averageNumbers(numbers) { + if (numbers.length === 0) return null; // Return null for empty array + return sumNumbers(numbers) / numbers.length; +} // Level 2: Array of strings const wordsArr = ['seat', 'correspond', 'linen', 'motif', 'hole', 'smell', 'smart', 'chaos', 'fuel', 'palace']; -function averageWordLength() { } +function averageWordLength(words) { + if (words.length === 0) return null; // Return null for empty array + let totalLength = sum(words.map(word => word.length)); + return totalLength / words.length; +} // Bonus - Iteration #4.1 -function avg() {} +function avg(arr) { + if (arr.length === 0) return null; // Return null for empty array + const total = sum(arr); + return total / arr.length; +} // Iteration #5: Unique arrays const wordsUnique = [ @@ -52,15 +92,28 @@ const wordsUnique = [ 'bring' ]; -function uniquifyArray() {} - +function uniquifyArray(words) { + if (words.length === 0) return null; // Return null for empty array + const uniqueWords = []; + for (let word of words) { + if (!uniqueWords.includes(word)) { + uniqueWords.push(word); + } + } + return uniqueWords; +} // Iteration #6: Find elements const wordsFind = ['machine', 'subset', 'trouble', 'starting', 'matter', 'eating', 'truth', 'disobedience']; -function doesWordExist() {} - +function doesWordExist(words, wordToSearch) { + if (words.length === 0) return null; // Return null for empty array + for (let word of words) { + if (word === wordToSearch) return true; + } + return false; +} // Iteration #7: Count repetition @@ -78,7 +131,13 @@ const wordsCount = [ 'matter' ]; -function howManyTimes() {} +function howManyTimes(words, wordToCount) { + let count = 0; + for (let word of words) { + if (word === wordToCount) count++; + } + return count; +} @@ -106,7 +165,29 @@ 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(matrix) { + let maxProduct = 0; + + // Check horizontally and vertically for maximum product of four adjacent numbers. + for (let i = 0; i < matrix.length; i++) { + for (let j = 0; j < matrix[i].length; j++) { + // Horizontal product + if (j < matrix[i].length - 3) { + const horizontalProduct = + matrix[i][j] * matrix[i][j + 1] * matrix[i][j + 2] * matrix[i][j + 3]; + maxProduct = Math.max(maxProduct, horizontalProduct); + } + // Vertical product + if (i < matrix.length - 3) { + const verticalProduct = + matrix[i][j] * matrix[i + 1][j] * matrix[i + 2][j] * matrix[i + 3][j]; + maxProduct = Math.max(maxProduct, verticalProduct); + } + } + } + + return maxProduct; // Ensure this returns the maximum product found +}