diff --git a/src/functions-and-arrays.js b/src/functions-and-arrays.js index 3a7dbec..372f929 100644 --- a/src/functions-and-arrays.js +++ b/src/functions-and-arrays.js @@ -1,41 +1,83 @@ // Iteration #1: Find the maximum -function maxOfTwoNumbers() {} - - +function maxOfTwoNumbers(a, b) { + return a > b ? a : b; +} +console.log(maxOfTwoNumbers(2,4)); // 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 longest = words[0]; + for (let word of words) { + if (word.length > longest.length) { + longest = word; + } + } + return longest; +} +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; + for (let number of numbers) { + sum += number; + } + return sum; +} +console.log(sumNumbers(numbers)); // 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"); + } + } + return total; +} +console.log(sum(numbers)); // Iteration #4: Calculate the average // Level 1: Array of numbers const numbersAvg = [2, 6, 9, 10, 7, 4, 1, 9]; -function averageNumbers() {} - +function averageNumbers(numbersAvg) { + if (numbersAvg.length === 0) return null; + return sumNumbers(numbersAvg) / numbersAvg.length; +} +console.log(averageNumbers(numbersAvg)); // Level 2: Array of strings const wordsArr = ['seat', 'correspond', 'linen', 'motif', 'hole', 'smell', 'smart', 'chaos', 'fuel', 'palace']; -function averageWordLength() { } +function averageWordLength(wordsArr) { + if (wordsArr.length === 0) return null; + let totalLength = 0; + for (let word of wordsArr) { + totalLength += word.length; + } + return totalLength / wordsArr.length; +} +console.log(averageWordLength(wordsArr)); // Bonus - Iteration #4.1 -function avg() {} +function avg(arr) { + if (arr.length === 0) return null; + return sum(arr) / arr.length; +} // Iteration #5: Unique arrays const wordsUnique = [ @@ -52,16 +94,26 @@ const wordsUnique = [ 'bring' ]; -function uniquifyArray() {} - - +function uniquifyArray(wordsUnique) { + if (wordsUnique.length === 0) return null; + let uniqueArray = []; + for (let word of wordsUnique) { + if (!uniqueArray.includes(word)) { + uniqueArray.push(word); + } + } + return uniqueArray; +} +console.log(uniquifyArray(wordsUnique)); // Iteration #6: Find elements const wordsFind = ['machine', 'subset', 'trouble', 'starting', 'matter', 'eating', 'truth', 'disobedience']; -function doesWordExist() {} - - +function doesWordExist(wordsFind, wordToFind) { + if (wordsFind.length === 0) return null; + return wordsFind.includes(wordToFind); +} +console.log(doesWordExist(wordsFind)); // Iteration #7: Count repetition const wordsCount = [ @@ -78,7 +130,19 @@ const wordsCount = [ 'matter' ]; -function howManyTimes() {} +function howManyTimes(wordsCount, wordToCount) { + let count = 0; + for (let word of wordsCount) { + if (word === wordToCount) { + count++; + } + } + return count; +} + +console.log(howManyTimes(wordsCount,"truth")); + + @@ -106,7 +170,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; + + for (let i = 0; i < matrix.length; i++) { + for (let j = 0; j < matrix[i].length - 3; j++) { + // Horizontal products + let horizontalProduct = matrix[i][j] * matrix[i][j + 1] * matrix[i][j + 2] * matrix[i][j + 3]; + if (horizontalProduct > maxProduct) { + maxProduct = horizontalProduct; + } + + // Vertical products + if (i < matrix.length - 3) { + let verticalProduct = matrix[i][j] * matrix[i + 1][j] * matrix[i + 2][j] * matrix[i + 3][j]; + if (verticalProduct > maxProduct) { + maxProduct = verticalProduct; + } + } + } + } + return maxProduct; +} +