diff --git a/src/functions-and-arrays.js b/src/functions-and-arrays.js index 3a7dbec..c089699 100644 --- a/src/functions-and-arrays.js +++ b/src/functions-and-arrays.js @@ -1,24 +1,66 @@ // Iteration #1: Find the maximum -function maxOfTwoNumbers() {} +function maxOfTwoNumbers(num1, num2) { + return Math.max(num1, num2) +} +const result = maxOfTwoNumbers(20, 55); +console.log(result); // Iteration #2: Find longest word const words = ['mystery', 'brother', 'aviator', 'crocodile', 'pearl', 'orchard', 'crackpot']; -function findLongestWord() {} +function findLongestWord(words) { + let longestWord = ""; + for(const word of words){ + if(word.length > longestWord.length){ + longestWord = word + } + } + return longestWord; +} + +const longest = findLongestWord(words); +console.log(longest); // 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 num of numbers){ + sum += num; + } + return sum; +} + +const totalSum = sumNumbers(numbers) +console.log(totalSum); // Iteration #3.1 Bonus: -function sum() {} +function sum(myArr) { + total = 0; + for (const item of myArr) { + if(typeof item === "string"){ + total += item.length + } + else if(typeof item === "number"){ + total += item + } + else if(typeof item === "boolean"){ + total += item ? 1:0; + } + } + return total +} + +const mixedArr = [6, 12, 'miami', 1, true, 'barca', '200', 'lisboa', 8, 10]; +const SumArr = sum(mixedArr); +console.log(SumArr); @@ -26,16 +68,38 @@ function sum() {} // Level 1: Array of numbers const numbersAvg = [2, 6, 9, 10, 7, 4, 1, 9]; -function averageNumbers() {} +function averageNumbers(num) { + const totalSum = sumNumbers(num) + return totalSum/num.length +} +const Average = averageNumbers(numbersAvg) +console.log(Average); // Level 2: Array of strings const wordsArr = ['seat', 'correspond', 'linen', 'motif', 'hole', 'smell', 'smart', 'chaos', 'fuel', 'palace']; -function averageWordLength() { } +function averageWordLength(words) { + totalwords = 0; + for (const item of words) { + if(typeof item === "string"){ + totalwords += item.length + } + } +return totalwords + } + const avgWordLength = averageWordLength(wordsArr)/wordsArr.length; + console.log(avgWordLength); // Bonus - Iteration #4.1 -function avg() {} +function avg(arr) { + const total = sum(arr); + return total/arr.length +} + +const mixArr = [6, 12, 'miami', 1, true, 'barca', '200', 'lisboa', 8, 10]; +const mixedAvg = avg(mixArr); +console.log(mixedAvg); // Iteration #5: Unique arrays const wordsUnique = [ @@ -52,14 +116,40 @@ const wordsUnique = [ 'bring' ]; -function uniquifyArray() {} - +function uniquifyArray(words) { + const Uniqueword = []; + for(const word of words){ + if(!Uniqueword.includes(word)){ + Uniqueword.push(word); + } + } + return Uniqueword +} +const Uniquewords = uniquifyArray(wordsUnique) +console.log(Uniquewords) // Iteration #6: Find elements const wordsFind = ['machine', 'subset', 'trouble', 'starting', 'matter', 'eating', 'truth', 'disobedience']; -function doesWordExist() {} +function doesWordExist(word, existWord) { + for (const words of word) { + if (words === existWord) { + return true; + } + } + return false; +} + +const existWord1 = 'eating'; +console.log(doesWordExist(wordsFind, existWord1)); + +const existWord2 = 'subset'; +console.log(doesWordExist(wordsFind, existWord2)); + +const existWord3 = 'Subset'; +console.log(doesWordExist(wordsFind, existWord3)); + @@ -78,7 +168,22 @@ const wordsCount = [ 'matter' ]; -function howManyTimes() {} +function howManyTimes(words, existWord) { + let count = 0; + for(const word of words){ + if( word === existWord){ + count++; + } + } + return count; +} + +const Word1 = 'starting'; +console.log(howManyTimes(wordsCount, Word1)) + +const Word2 = 'matter'; +console.log(howManyTimes(wordsCount, Word2)); + @@ -106,8 +211,48 @@ 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; j++) { + // Check horizontally to the right + if (j <= matrix[i].length - 4) { + let horizontalProduct = matrix[i][j] * matrix[i][j + 1] * matrix[i][j + 2] * matrix[i][j + 3]; + if (horizontalProduct > maxProduct) { + maxProduct = horizontalProduct; + } + } + + // Check vertically downward + if (i <= matrix.length - 4) { + let verticalProduct = matrix[i][j] * matrix[i + 1][j] * matrix[i + 2][j] * matrix[i + 3][j]; + if (verticalProduct > maxProduct) { + maxProduct = verticalProduct; + } + } + + // Check diagonally to the bottom-right + if (i <= matrix.length - 4 && j <= matrix[i].length - 4) { + let diagonalRightProduct = matrix[i][j] * matrix[i + 1][j + 1] * matrix[i + 2][j + 2] * matrix[i + 3][j + 3]; + if (diagonalRightProduct > maxProduct) { + maxProduct = diagonalRightProduct; + } + } + + // Check diagonally to the bottom-left + if (i <= matrix.length - 4 && j >= 3) { + let diagonalLeftProduct = matrix[i][j] * matrix[i + 1][j - 1] * matrix[i + 2][j - 2] * matrix[i + 3][j - 3]; + if (diagonalLeftProduct > maxProduct) { + maxProduct = diagonalLeftProduct; + } + } + } + } + + return maxProduct; +} + console.log(greatestProduct(matrix));