diff --git a/src/functions-and-arrays.js b/src/functions-and-arrays.js index 3a7dbec41..88a2505b8 100644 --- a/src/functions-and-arrays.js +++ b/src/functions-and-arrays.js @@ -1,41 +1,91 @@ // Iteration #1: Find the maximum -function maxOfTwoNumbers() {} - +function maxOfTwoNumbers(num1, num2) { + if (num1 > num2) { + return num1; + } + return num2; +} +console.log(maxOfTwoNumbers(4, 8)); // Iteration #2: Find longest word const words = ['mystery', 'brother', 'aviator', 'crocodile', 'pearl', 'orchard', 'crackpot']; -function findLongestWord() {} - +function findLongestWord(array) { + longestWord = ""; + if (!array.length) return null; + for (const word of array) { + if (word.length > longestWord.length) { + longestWord = word; + } + } + 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(numArray) { + if (!numArray.length) return 0; + let sum = 0; + for (const element of numArray) { + sum += element; + } + return sum; +} +console.log(sumNumbers(numbers)); // Iteration #3.1 Bonus: -function sum() {} - +const mixedArr = [6, 12, 'miami', 1, true, 'barca', '200', 'lisboa', 8, 10]; +function sum(mixArray) { + if (!mixArray.length) return 0; + let sum = 0; + for (const element of mixArray) { + if (typeof element === 'number') { + sum += element; + } else if (typeof element === 'string') { + sum += element.length; + } else if (typeof element === 'boolean') { + sum += Number(element); + } else { + throw new Error('One of the values is a not supported value') + } + } + return sum; +} +console.log(sum(mixedArr)); // Iteration #4: Calculate the average // Level 1: Array of numbers const numbersAvg = [2, 6, 9, 10, 7, 4, 1, 9]; -function averageNumbers() {} - - +function averageNumbers(numAvgArray) { + if (!numAvgArray.length) return null; + return sumNumbers(numAvgArray) / numAvgArray.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(wordAvgArray) { + if (!wordAvgArray.length) return null; + let arrayLength = 0; + for (const word of wordAvgArray) { + arrayLength += word.length; + } + return arrayLength / wordAvgArray.length; +} +console.log(averageWordLength(wordsArr)); // Bonus - Iteration #4.1 -function avg() {} +function avg(mixArray) { + if (!mixArray.length) return null; + return sum(mixArray) / mixArray.length; +} +console.log(avg(mixedArr)); // Iteration #5: Unique arrays const wordsUnique = [ @@ -52,15 +102,36 @@ const wordsUnique = [ 'bring' ]; -function uniquifyArray() {} - +function uniquifyArray(stringsArray) { + if (!stringsArray.length) return null; + const indexOfDuplicates = []; + for (let i = stringsArray.length - 1; i >= 0; i--) { + const firstTime = stringsArray.indexOf(stringsArray[i], i + 1); + if (firstTime !== -1) { + indexOfDuplicates.push(firstTime); + } + } + console.log(indexOfDuplicates) + for (const index of indexOfDuplicates) { + stringsArray.splice(index, 1); + } + return stringsArray; +} +console.log(uniquifyArray(wordsUnique)); // Iteration #6: Find elements const wordsFind = ['machine', 'subset', 'trouble', 'starting', 'matter', 'eating', 'truth', 'disobedience']; -function doesWordExist() {} +function doesWordExist(wordsArray, word) { + if (!wordsArray.length) return null; + if (typeof word != 'string') { + throw new Error('The word to find is not a String') + } + return wordsArray.includes(word); +} +console.log(doesWordExist(wordsFind, 'subset')) // Iteration #7: Count repetition @@ -78,8 +149,20 @@ const wordsCount = [ 'matter' ]; -function howManyTimes() {} - +function howManyTimes(wordsArray, word) { + if (!wordsArray.length) return 0; + if (typeof word != 'string') { + throw new Error('The word to find is not a String') + } + let wordRep = 0; + for (const element of wordsArray) { + if (element === word) { + wordRep++; + } + } + return wordRep; +} +console.log(howManyTimes(wordsCount, 'matter')) // Iteration #8: Bonus @@ -106,10 +189,58 @@ 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(bigMatrix) { + const rows = bigMatrix.length; + const columns = bigMatrix[0].length; + let greatestProd = 0; + + for (let i = 0; i < rows; i++) { + for (let j = 0; j <= columns - 4; j++) { + let rowsProd = bigMatrix[i][j] * bigMatrix[i][j + 1] * bigMatrix[i][j + 2] * bigMatrix[i][j + 3]; + greatestProd = Math.max(greatestProd, rowsProd); + } + } + for (let i = 0; i <= rows - 4; i++) { + for (let j = 0; j < columns; j++) { + let columnsProd = bigMatrix[i][j] * bigMatrix[i + 1][j] * bigMatrix[i + 2][j] * bigMatrix[i + 3][j]; + greatestProd = Math.max(greatestProd, columnsProd); + } + } + return greatestProd; +} +console.log(greatestProduct(matrix)); + +//Iteraction #8.2 Bonus +function greatestProductOfDiagonals(bigMatrix) { + const rows = bigMatrix.length; + const columns = bigMatrix[0].length; + let greatestProd = 0; + + + for (let i = 3; i < rows; i++) { + for (let j = 3; j < columns; j++) { + if (i - 3 >= 0 && j - 3 >= 0) { + let diagonalULProd = bigMatrix[i][j] * bigMatrix[i - 1][j - 1] * bigMatrix[i - 2][j - 2] * bigMatrix[i - 3][j - 3]; + greatestProd = Math.max(greatestProd, diagonalULProd); + } + if (i - 3 >= 0 && j + 3 < columns) { + let diagonalURProd = bigMatrix[i][j] * bigMatrix[i - 1][j + 1] * bigMatrix[i - 2][j + 2] * bigMatrix[i - 3][j + 3]; + greatestProd = Math.max(greatestProd, diagonalURProd); + } + if (i + 3 < rows && j - 3 >= 0) { + let diagonalDLProd = bigMatrix[i][j] * bigMatrix[i + 1][j - 1] * bigMatrix[i + 2][j - 2] * bigMatrix[i + 3][j - 3]; + greatestProd = Math.max(greatestProd, diagonalDLProd); + } + if (i + 3 < rows && j + 3 < columns) { + let diagonalDRProd = bigMatrix[i][j] * bigMatrix[i + 1][j + 1] * bigMatrix[i + 2][j + 2] * bigMatrix[i + 3][j + 3]; + greatestProd = Math.max(greatestProd, diagonalDRProd); + } + } + } + return greatestProd; +} +console.log(greatestProductOfDiagonals(matrix)); // The following is required to make unit tests work. /* Environment setup. Do not modify the below code. */