From dff39d3827f5969d6a8df8bcfd0dac9d78cf94ea Mon Sep 17 00:00:00 2001 From: AnaRibeiroPT Date: Thu, 19 Oct 2023 20:14:28 +0100 Subject: [PATCH] Solved lab --- src/functions-and-arrays.js | 105 +++++++++++++++++++++++++++++------- 1 file changed, 86 insertions(+), 19 deletions(-) diff --git a/src/functions-and-arrays.js b/src/functions-and-arrays.js index 3a7dbec41..2d663c2ba 100644 --- a/src/functions-and-arrays.js +++ b/src/functions-and-arrays.js @@ -1,21 +1,38 @@ // 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(words) { + if (words.length === 0) { + return null; + } + let longestWord = words[0]; + words.forEach(function(word) { + if (word.length > longestWord.length) { + longestWord = word; + } + }); + return longestWord; +} // 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; + numbers.forEach(function(element) { + sum += element; + }); + return sum; +} // Iteration #3.1 Bonus: function sum() {} @@ -26,13 +43,38 @@ function sum() {} // 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; + + numbersAvg.forEach(function(element) { + sum += element; + }); + + const avg = sum/numbersAvg.length; + + return avg; +} // 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; + wordsArr.forEach(function(element) { + sum += element.length; + }); + const avg = sum/wordsArr.length; + + return avg; +} // Bonus - Iteration #4.1 function avg() {} @@ -52,16 +94,34 @@ const wordsUnique = [ 'bring' ]; -function uniquifyArray() {} - - +function uniquifyArray(wordsUnique) { + if(wordsUnique.length === 0) { + return null; + } + let unique = []; + wordsUnique.forEach(function(element) { + if (unique.indexOf(element) === -1) { + unique.push(element); + } + }); + 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; + } + let words = false; + wordsFind.forEach(function(element) { + if (element === word) { + words = true; + } + }); + return words; +} // Iteration #7: Count repetition const wordsCount = [ @@ -78,9 +138,16 @@ const wordsCount = [ 'matter' ]; -function howManyTimes() {} - +function howManyTimes(wordsCount, word) { + let counting = 0; + wordsCount.forEach(function(element) { + if (element === word) { + counting++; + } + }); + return counting; +} // Iteration #8: Bonus const matrix = [