From efbdef66395bbbb85d0b28e9ff625622e004806d Mon Sep 17 00:00:00 2001 From: VictorMartins Date: Thu, 19 Sep 2024 12:58:54 +0200 Subject: [PATCH 1/3] Half Part --- src/functions-and-arrays.js | 35 +++++++++++++++++++++++++++++++---- 1 file changed, 31 insertions(+), 4 deletions(-) diff --git a/src/functions-and-arrays.js b/src/functions-and-arrays.js index 3a7dbec41..bb7ecc597 100644 --- a/src/functions-and-arrays.js +++ b/src/functions-and-arrays.js @@ -1,19 +1,38 @@ // Iteration #1: Find the maximum -function maxOfTwoNumbers() {} +function maxOfTwoNumbers(number1, number2) { + if (number1 === number2) + return number1; + else { + if (number1 > number2) + return number1; + else + return number2; + } +} // Iteration #2: Find longest word const words = ['mystery', 'brother', 'aviator', 'crocodile', 'pearl', 'orchard', 'crackpot']; -function findLongestWord() {} +function findLongestWord(paramArray) { + switch (paramArray.length){ + case 0: return null; + case 1: return words[0]; + default: + } +} // Iteration #3: Calculate the sum const numbers = [6, 12, 1, 18, 13, 16, 2, 1, 8, 10]; -function sumNumbers() {} +function sumNumbers(paramArray) { + if (paramArray.length>0) + return paramArray.reduce(function (totalSum, element) { return totalSum + element}); + return 0; +} @@ -26,8 +45,16 @@ function sum() {} // Level 1: Array of numbers const numbersAvg = [2, 6, 9, 10, 7, 4, 1, 9]; -function averageNumbers() {} +function averageNumbers(paramArray) { + let numberElements = paramArray.length +// console.log(numberElements); Don't needed anymore + if (numberElements === 0) + return null; + let sumArrays = paramArray.reduce(function (totalSum, element) { return totalSum + element}); + return sumArrays/numberElements; +} +console.log(averageNumbers([])); //For testing an Empty array // Level 2: Array of strings const wordsArr = ['seat', 'correspond', 'linen', 'motif', 'hole', 'smell', 'smart', 'chaos', 'fuel', 'palace']; From 1ac5136c4acf4fae2ef82cffc8737c7e175d7827 Mon Sep 17 00:00:00 2001 From: VictorMartins Date: Fri, 20 Sep 2024 13:22:05 +0200 Subject: [PATCH 2/3] Near End --- src/functions-and-arrays.js | 82 +++++++++++++++++++++++++++++++++---- 1 file changed, 73 insertions(+), 9 deletions(-) diff --git a/src/functions-and-arrays.js b/src/functions-and-arrays.js index bb7ecc597..a12248e03 100644 --- a/src/functions-and-arrays.js +++ b/src/functions-and-arrays.js @@ -16,10 +16,17 @@ function maxOfTwoNumbers(number1, number2) { const words = ['mystery', 'brother', 'aviator', 'crocodile', 'pearl', 'orchard', 'crackpot']; function findLongestWord(paramArray) { + let longestWord = ""; switch (paramArray.length){ case 0: return null; case 1: return words[0]; default: + paramArray.forEach(element => { + if (element.length > longestWord) + longestWord = element; + } + ) + return longestWord; } } @@ -29,7 +36,7 @@ function findLongestWord(paramArray) { const numbers = [6, 12, 1, 18, 13, 16, 2, 1, 8, 10]; function sumNumbers(paramArray) { - if (paramArray.length>0) + if (paramArray.length >0 ) return paramArray.reduce(function (totalSum, element) { return totalSum + element}); return 0; } @@ -37,7 +44,21 @@ function sumNumbers(paramArray) { // Iteration #3.1 Bonus: -function sum() {} +function sum(paramArray) { + if (paramArray.length === 0) { + return 0; + } + + let totalSum = 0; + paramArray.forEach(element => { + if (typeof(element) !== numbers) + throw new Error("Error message goes here"); + else + totalSum += element; + } + ); + return totalSum; +} @@ -54,15 +75,42 @@ function averageNumbers(paramArray) { return sumArrays/numberElements; } -console.log(averageNumbers([])); //For testing an Empty array +// +//console.log(averageNumbers([])); //For testing an Empty array +// + // Level 2: Array of strings const wordsArr = ['seat', 'correspond', 'linen', 'motif', 'hole', 'smell', 'smart', 'chaos', 'fuel', 'palace']; -function averageWordLength() { } +function averageWordLength(paramArray) { + if (paramArray.length === 0) { + return null; + } + let totalLength = 0; + paramArray.forEach(element => { totalLength += element.length }); + return totalLength/paramArray.length; + } // Bonus - Iteration #4.1 -function avg() {} +function avg(paramArray) { + if (paramArray.length === 0) { + return null; + } + let sumElement = 0; + paramArray.forEach(element => { + if (typeof(element) === "boolean") + if (element === true) + sumElement += 1; + if (typeof(element) === "string"){ + sumElement += element.length; + } + if (typeof(element) === "number") + sumElement += element; + }) + return sumElement/paramArray.length; +} +console.log(avg([6, 12, 'miami', 1, true, 'barca', '200', 'lisboa', 8, 10])) // Iteration #5: Unique arrays const wordsUnique = [ @@ -79,14 +127,22 @@ const wordsUnique = [ 'bring' ]; -function uniquifyArray() {} +function uniquifyArray(paramArray) { + if (paramArray.length === 0) { + return null; + } +} // Iteration #6: Find elements const wordsFind = ['machine', 'subset', 'trouble', 'starting', 'matter', 'eating', 'truth', 'disobedience']; -function doesWordExist() {} +function doesWordExist(paramArray) { + if (paramArray.length === 0) { + return null; + } +} @@ -105,7 +161,11 @@ const wordsCount = [ 'matter' ]; -function howManyTimes() {} +function howManyTimes(paramArray) { + if (paramArray.length === 0) { + return 0; + } +} @@ -133,7 +193,11 @@ 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(paramArray) { + if (paramArray.length === 0) { + return 0; + } +} From 95a879f0fc22c9c28f4ea7a84de836c5d7b74c69 Mon Sep 17 00:00:00 2001 From: VictorMartins Date: Sun, 22 Sep 2024 01:15:05 +0200 Subject: [PATCH 3/3] Solved lab --- src/functions-and-arrays.js | 48 +++++++++++++++++++++++++++---------- 1 file changed, 35 insertions(+), 13 deletions(-) diff --git a/src/functions-and-arrays.js b/src/functions-and-arrays.js index a12248e03..0548a67ec 100644 --- a/src/functions-and-arrays.js +++ b/src/functions-and-arrays.js @@ -19,7 +19,7 @@ function findLongestWord(paramArray) { let longestWord = ""; switch (paramArray.length){ case 0: return null; - case 1: return words[0]; + case 1: return paramArray[0]; default: paramArray.forEach(element => { if (element.length > longestWord) @@ -30,8 +30,6 @@ function findLongestWord(paramArray) { } } - - // Iteration #3: Calculate the sum const numbers = [6, 12, 1, 18, 13, 16, 2, 1, 8, 10]; @@ -42,26 +40,32 @@ function sumNumbers(paramArray) { } - // Iteration #3.1 Bonus: function sum(paramArray) { if (paramArray.length === 0) { return 0; } - + let itZero = true; let totalSum = 0; + paramArray.forEach(element => { - if (typeof(element) !== numbers) + let value = element; + if (typeof(element) === "boolean" && element === true) + value = 1; + if (typeof(element) === "string") + value = element.length; + if (typeof(element) === "object") throw new Error("Error message goes here"); - else - totalSum += element; + if (element !== 0 && itZero === true) + itZero = false; + totalSum += value; } ); + if (itZero === true) + return 0; return totalSum; } - - // Iteration #4: Calculate the average // Level 1: Array of numbers const numbersAvg = [2, 6, 9, 10, 7, 4, 1, 9]; @@ -110,7 +114,6 @@ function avg(paramArray) { }) return sumElement/paramArray.length; } -console.log(avg([6, 12, 'miami', 1, true, 'barca', '200', 'lisboa', 8, 10])) // Iteration #5: Unique arrays const wordsUnique = [ @@ -128,9 +131,16 @@ const wordsUnique = [ ]; function uniquifyArray(paramArray) { + // not working not descovered if (paramArray.length === 0) { return null; } + console.log(paramArray); + let newArray = paramArray.filter(function(element){ + return paramArray.includes(element); + }) + console.log(newArray); + return newArray; } @@ -138,10 +148,17 @@ function uniquifyArray(paramArray) { // Iteration #6: Find elements const wordsFind = ['machine', 'subset', 'trouble', 'starting', 'matter', 'eating', 'truth', 'disobedience']; -function doesWordExist(paramArray) { +function doesWordExist(paramArray, element2Find) { if (paramArray.length === 0) { return null; } + // Dont need anymore, simpler with includes + // let isElementFound = false; + // paramArray.forEach(element => { + // if (element === element2Find) + // isElementFound = false; + // }); +return paramArray.includes(element2Find); } @@ -161,10 +178,15 @@ const wordsCount = [ 'matter' ]; -function howManyTimes(paramArray) { +function howManyTimes(paramArray, element) { if (paramArray.length === 0) { return 0; } + let repetition = 0; + for (let i = 0; i < paramArray.length; i++) + if (paramArray[i] === element) + repetition++; + return repetition; }