From 4126c1d89555a029c325335b3c0f549bafd2f4a0 Mon Sep 17 00:00:00 2001 From: Yerko Arce Date: Wed, 8 Jan 2025 00:56:18 -0300 Subject: [PATCH] Solved lab --- src/functions-and-arrays.js | 142 ++++++++++++++++++++++++++++++++---- 1 file changed, 127 insertions(+), 15 deletions(-) diff --git a/src/functions-and-arrays.js b/src/functions-and-arrays.js index 3a7dbec..48a14e5 100644 --- a/src/functions-and-arrays.js +++ b/src/functions-and-arrays.js @@ -1,41 +1,111 @@ // Iteration #1: Find the maximum -function maxOfTwoNumbers() {} - - +function maxOfTwoNumbers(a, b) { + if (a > b){ + return a + } + return b +} // Iteration #2: Find longest word const words = ['mystery', 'brother', 'aviator', 'crocodile', 'pearl', 'orchard', 'crackpot']; -function findLongestWord() {} +function findLongestWord(words) { + let longest = '' + if (words.length === 0){ + return null + } + for (let i = 0; i < words.length; i++){ + if (words[i].length > longest.length){ + longest = words[i] + } + } + return 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 i = 0; i < numbers.length; i++){ + sum += numbers[i] + } + return sum +} // Iteration #3.1 Bonus: -function sum() {} - +const mixedArr = [6, 12, 'miami', 1, true, 'barca', '200', 'lisboa', 8, 10]; + +function sum(mixedArr) { + let sum = 0; + for (let i = 0; i < mixedArr.length; i++){ + if (typeof mixedArr[i] === 'number' || typeof mixedArr[i] === 'boolean'){ + sum += mixedArr[i] + } else if (typeof mixedArr[i] === 'string'){ + sum += mixedArr[i].length + } else if (typeof mixedArr[i] === 'object') { + throw new Error("Unsupported data type sir or ma'am") + } + } + + return sum +} // 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 + } + + let sum = 0; + for (let i = 0; i < numbersAvg.length; i++){ + sum += numbersAvg[i] + } + return sum/numbersAvg.length +} // 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 sum = 0; + for (let i = 0; i < wordsArr.length; i++){ + sum += wordsArr[i].length + } + return sum/wordsArr.length +} // Bonus - Iteration #4.1 -function avg() {} +function avg(arr) { + if (arr.length === 0){ + return null + } + + let sum = 0; + for (let i = 0; i < arr.length; i++){ + if (typeof arr[i] === 'object'){ + throw new Error('error') + } else if (typeof arr[i] === 'string'){ + sum += arr[i].length + } else { + sum += arr[i] + } + } + return sum/arr.length +} // Iteration #5: Unique arrays const wordsUnique = [ @@ -52,14 +122,31 @@ const wordsUnique = [ 'bring' ]; -function uniquifyArray() {} - +function uniquifyArray(wordsUnique) { + let unique = [] + if (wordsUnique.length === 0){ + return null + } + for (let i = 0; i < wordsUnique.length; i++){ + if (!unique.includes(wordsUnique[i])){ + unique.push(wordsUnique[i]) + } + } + return unique +} // Iteration #6: Find elements const wordsFind = ['machine', 'subset', 'trouble', 'starting', 'matter', 'eating', 'truth', 'disobedience']; -function doesWordExist() {} +function doesWordExist(wordsFind, word) { + if (wordsFind.length === 0){ + return null + } else if (wordsFind.includes(word)){ + return true + } + return false +} @@ -78,7 +165,17 @@ const wordsCount = [ 'matter' ]; -function howManyTimes() {} +function howManyTimes(wordsCount, word) { + let count = 0 + + for (let i = 0; i < wordsCount.length; i++){ + if (wordsCount[i] === word){ + count++ + } + } + + return count +} @@ -106,7 +203,22 @@ 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 greatestProduct = 0 + for (let i = 0; i < (matrix.length - 3); i++) { + for (let j = 0; j < (matrix[i].length - 3);j++){ + let horizontalProduct = (matrix[i][j]*matrix[i][j+1]*matrix[i][j+2]*matrix[i][j+3]) + let verticalProduct = (matrix[i][j]*matrix[i+1][j]*matrix[i+2][j]*matrix[i+3][j]) + if (horizontalProduct > greatestProduct){ + greatestProduct = horizontalProduct + } else if (verticalProduct > greatestProduct){ + greatestProduct = verticalProduct + } + } + } + + return greatestProduct +}