From 9d0bf3c4cc630b6f33ca5d284fa1991b92bd5c02 Mon Sep 17 00:00:00 2001 From: Rodrigo Sotolani Date: Thu, 20 Oct 2022 18:36:43 -0300 Subject: [PATCH] exercicio completo do day04 --- src/functions-and-arrays.js | 160 +++++++++++++++++++++++++++++++++--- 1 file changed, 148 insertions(+), 12 deletions(-) diff --git a/src/functions-and-arrays.js b/src/functions-and-arrays.js index 3a7dbec41..7fd30903d 100644 --- a/src/functions-and-arrays.js +++ b/src/functions-and-arrays.js @@ -1,24 +1,70 @@ // Iteration #1: Find the maximum -function maxOfTwoNumbers() {} +function maxOfTwoNumbers(num1, num2) { + if ( num1 > num2 ) { + return num1; + } else { + return num2; + } +} +console.log(1, maxOfTwoNumbers(20,10)); // Iteration #2: Find longest word const words = ['mystery', 'brother', 'aviator', 'crocodile', 'pearl', 'orchard', 'crackpot']; +//const words = []; + +function findLongestWord(words) { + let longest = ""; + if (!words.length) { + return null; + } else { + for (let i of words) { + if (longest.length < i.length) longest = i; + } + return longest; + } +} -function findLongestWord() {} +console.log(2, 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 soma = 0; + for (let i of numbers) { + soma += i; + } + return soma; +} +console.log(3, sumNumbers(numbers)); // Iteration #3.1 Bonus: -function sum() {} +function sum(array) { + let soma = 0; + for (let i of array) { + switch (typeof(i)) { + case "number": + case "boolean": + soma += i; + break; + case "string": + soma += i.length; + break; + case "object": + throw new Error ("Unsupported data type sir or ma'am"); + break; + } + } + return soma; +} + +const mixedArr = [6, 12, 'miami', 1, true, 'barca', '200', 'lisboa', 8, 10, [9, 9], { "a": 89}]; +console.log(3.1, sum(mixedArr)); //57 @@ -26,16 +72,52 @@ function sum() {} // Level 1: Array of numbers const numbersAvg = [2, 6, 9, 10, 7, 4, 1, 9]; -function averageNumbers() {} +function averageNumbers(numeros) { + media = null; + if (!numeros.length) { + return null; + } else { + for (let i of numeros) { + media += i; + } + media /= numeros.length; + return media; + } +} +console.log(4.1, averageNumbers(numbersAvg)); // Level 2: Array of strings const wordsArr = ['seat', 'correspond', 'linen', 'motif', 'hole', 'smell', 'smart', 'chaos', 'fuel', 'palace']; -function averageWordLength() { } +function averageWordLength(words) { + media = null; + if (!words.length) { + return null; + } else { + for (let i of words) { + media += i.length; + } + media /= words.length; + return media; + } + +} +console.log(4.2, averageWordLength(wordsArr)); + // Bonus - Iteration #4.1 -function avg() {} +function avg(mixedArr) { + media = null; + if (!mixedArr.length) { + return null; + } else { + media = sum(mixedArr) / mixedArr.length; + return media; + } +} +console.log("bonus 4.1", avg(mixedArr)); + // Iteration #5: Unique arrays const wordsUnique = [ @@ -52,15 +134,36 @@ const wordsUnique = [ 'bring' ]; -function uniquifyArray() {} +function uniquifyArray(array) { + const newArray = []; + if (!array.length) { + return null; + } else { + for (let i of array) { + if (!newArray.includes(i)) newArray.push(i); + } + return newArray; + } +} +console.log(5, uniquifyArray(wordsUnique)); // Iteration #6: Find elements const wordsFind = ['machine', 'subset', 'trouble', 'starting', 'matter', 'eating', 'truth', 'disobedience']; -function doesWordExist() {} +function doesWordExist(array, wordToSearch) { + if (!array.length) { + return null; + } else { + for (let i of array) { + if (array.includes(wordToSearch)) return true; + } + return false; + } +} +console.log(6, doesWordExist(wordsFind, 'trouble')); // Iteration #7: Count repetition @@ -78,7 +181,18 @@ const wordsCount = [ 'matter' ]; -function howManyTimes() {} +function howManyTimes(array, wordToSearch) { + let wordCount = 0; + if (!array.length) { + return 0; + } else { + for (let i of array) { + wordCount += doesWordExist(i, wordToSearch); + } + return wordCount; + } +} +console.log(7, howManyTimes(wordsCount, "matter")); @@ -106,7 +220,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(array) { + let produto = 0, produtoTemp = 0; + //percorrendo horizontalmente + for (let i = 0; i < array.length; i++){ + for (let j = 0; j < array.length - 3; j++) { + produtoTemp = array[i][j] * array[i][j+1] * array[i][j+2] * array[i][j+3]; + if (produtoTemp > produto) { produto = produtoTemp; } + } + } + + //percorrendo verticalmente + for (let i = 0; i < array.length - 3; i++){ + for (let j = 0; j < array.length; j++) { + produtoTemp = array[i][j] * array[i+1][j] * array[i+2][j] * array[i+3][j]; + if (produtoTemp > produto) { produto = produtoTemp; } + } + } + + return produto; + +} + +console.log(8, greatestProduct(matrix));