diff --git a/src/functions-and-arrays.js b/src/functions-and-arrays.js index 3a7dbec41..1f1f4b9a9 100644 --- a/src/functions-and-arrays.js +++ b/src/functions-and-arrays.js @@ -1,41 +1,156 @@ // Iteration #1: Find the maximum -function maxOfTwoNumbers() {} - - +function maxOfTwoNumbers(num1, num2) { + if (num1 > num2) { + return num1; + } else { + return num2; + } +} // Iteration #2: Find longest word const words = ['mystery', 'brother', 'aviator', 'crocodile', 'pearl', 'orchard', 'crackpot']; -function findLongestWord() {} +function findLongestWord(wordsArr) { + if (wordsArr.length === 0) { + return null; + + } + + let longest = ''; + + for (let i = 0; i < wordsArr.length; i ++) { + if (wordsArr[i].length > longest.length) { + longest = wordsArr[i]; + + } + } + + return longest; +} // Iteration #3: Calculate the sum const numbers = [6, 12, 1, 18, 13, 16, 2, 1, 8, 10]; -function sumNumbers() {} +function sumNumbers(numbersArray) { + let sum = 0; + + for(let i = 0; i < numbersArray.length; i++) { + sum += numbersArray[i]; + } + + return sum; +} // Iteration #3.1 Bonus: -function sum() {} +function sum(array) { + if (array.length === 0) { + return 0; + } + + let total = 0; + + for (let i = 0; i < array.length; i++) { + let item = array[i]; + + //Si es número + if (typeof item === 'number') { + total += item; + } + + + // Si es string + else if (typeof item === 'string') { + total += item.length; + } + + //Si es booleano + else if (typeof item === 'boolean') { + total += item ? 1 : 0; + } + // Si es tipo no soportado (objeto o array) + else { + throw new Error ("tipo de dato no admitido") + } + } + return total + +} // Iteration #4: Calculate the average // Level 1: Array of numbers -const numbersAvg = [2, 6, 9, 10, 7, 4, 1, 9]; +const numbersAvg = [2, 6, 9, 10, 7, 4, 1, 9]; + + + function averageNumbers(numbers) { + if (numbers.length === 0) { + return null; +} + +let sum = 0 + +for (let i = 0; i < numbers.length; i++) { + sum += numbers[i]; +} -function averageNumbers() {} +return sum / 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 (!Array.isArray(words) || words.length === 0) return null + + let totalLength = 0 + + for (let i = 0; i < words.length; i++) { + if (typeof words[i] !== 'string') throw new Error ("Tipo de entrada no válido") + totalLength += words[i].length; + } + + return totalLength / words.length; +} + + // Bonus - Iteration #4.1 -function avg() {} +function avg(arr) { + if (!Array.isArray(arr) || arr.length === 0) return null + + let total = 0; + let count = 0; + + for (let i = 0; i < arr.length; i++) { + let item = arr[i]; + + if (typeof item === 'number') { + total += item; + count++; + } else if (typeof item === 'string') { + total += item.length + count++; + } else if (typeof item === 'boolean') { + total += item ? 1 : 0; + count++; + } else { + throw new Error ("Tipo de datos no admitido"); + } + + } + + return total / count; + +} + + // Iteration #5: Unique arrays const wordsUnique = [ @@ -52,14 +167,30 @@ const wordsUnique = [ 'bring' ]; -function uniquifyArray() {} +function uniquifyArray(words) { + if (!Array.isArray(words) || words.length === 0) return null; + + const uniqueWords = []; + + for (let i = 0; i < words.length; i++) { + if (!uniqueWords.includes(words[i])) { + uniqueWords.push(words[i]); + } + + } + return uniqueWords +} // Iteration #6: Find elements const wordsFind = ['machine', 'subset', 'trouble', 'starting', 'matter', 'eating', 'truth', 'disobedience']; -function doesWordExist() {} +function doesWordExist(words, wordToFind) { + if (!Array.isArray(words) || words.length === 0) return null; + return words.includes(wordToFind); + +} @@ -78,7 +209,19 @@ const wordsCount = [ 'matter' ]; -function howManyTimes() {} +function howManyTimes(words, wordToCount) { + if (!Array.isArray(words) || words.length === 0) return 0; + + let count = 0; + + for (let i = 0; i < words.length; i++) { + if (words[i] === wordToCount) { + count++; + } + } + + return count; +} @@ -106,7 +249,44 @@ 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) { + if (!Array.isArray(matrix) || matrix.length === 0) return 0; + + let maxProduct = 0; + + for (let row = 0; row < matrix.length; row++) { + for (let col = 0; col < matrix[row].length; col++) { + + // Horizontal + if (col + 3 < matrix[row].length) { + const HorizProduct = + matrix[row][col] * + matrix[row][col + 1] * + matrix[row][col + 2] * + matrix[row][col + 3] + + if (HorizProduct > maxProduct) maxProduct = HorizProduct; + } + + + // Vertical + if (row + 3 < matrix.length) { + const vertProduct = + matrix[row][col] * + matrix[row + 1][col] * + matrix[row + 2][col] * + matrix[row + 3][col]; + + + if (vertProduct > maxProduct) maxProduct = vertProduct; + + } + } +} + +return maxProduct + +}