From 80387a01d99b1f94df87879df772a6e4ac2f914d Mon Sep 17 00:00:00 2001 From: Gustavo Fonte Date: Thu, 19 Oct 2023 17:47:28 +0100 Subject: [PATCH 1/2] done iterations 1-4.1 --- src/functions-and-arrays.js | 58 ++++++++++++++++++++++++++++++++----- 1 file changed, 50 insertions(+), 8 deletions(-) diff --git a/src/functions-and-arrays.js b/src/functions-and-arrays.js index 3a7dbec41..7bfca3fbb 100644 --- a/src/functions-and-arrays.js +++ b/src/functions-and-arrays.js @@ -1,32 +1,74 @@ // 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; +} +console.log(findLongestWord(words)) // Iteration #3: Calculate the sum const numbers = [6, 12, 1, 18, 13, 16, 2, 1, 8, 10]; -function sumNumbers() {} - +function sumNumbers(numbers) { + if(numbers.length === 0){ + return 0 + } + + let numSum = 0 + + numbers.forEach(function(number){ + numSum += number + }) + return numSum +} // Iteration #3.1 Bonus: function 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(avgSum){ + if (numbersAvg.length === 0){ + return null + } +function sumNumbers(numbersAvg) { + let avgSum = 0; + + numbersAvg.forEach(function (number) { + avgSum += number; + }); + return avgSum; +} + return sumNumbers(numbersAvg) / numbersAvg.length +} + + + + + console.log(averageNumbers(numbersAvg)) // Level 2: Array of strings From 5bdc49ae38a889d36d71172d7e4e23a49f01d073 Mon Sep 17 00:00:00 2001 From: Gustavo Fonte Date: Thu, 19 Oct 2023 19:47:30 +0100 Subject: [PATCH 2/2] done it1-5 --- src/functions-and-arrays.js | 80 +++++++++++++++++++++++++++---------- 1 file changed, 60 insertions(+), 20 deletions(-) diff --git a/src/functions-and-arrays.js b/src/functions-and-arrays.js index 7bfca3fbb..aa4dc7444 100644 --- a/src/functions-and-arrays.js +++ b/src/functions-and-arrays.js @@ -7,7 +7,7 @@ function maxOfTwoNumbers(num1,num2) { // Iteration #2: Find longest word -const words = ['mystery', 'brother', 'aviator', 'crocodile', 'pearl', 'orchard', 'crackpot']; +/*const words = ['mystery', 'brother', 'aviator', 'crocodile', 'pearl', 'orchard', 'crackpot']; function findLongestWord(words) { if (words.length === 0){ @@ -23,7 +23,7 @@ function findLongestWord(words) { } ) return longestWord; } -console.log(findLongestWord(words)) +console.log(findLongestWord(words)) */ // Iteration #3: Calculate the sum @@ -51,30 +51,34 @@ function sum() {} const numbersAvg = [2, 6, 9, 10, 7, 4, 1, 9]; function averageNumbers(avgSum){ - if (numbersAvg.length === 0){ - return null + if (avgSum.length === 0){ + return null; } -function sumNumbers(numbersAvg) { - let avgSum = 0; + function sumNumbers(numbersAvg) { + let avgSum = 0; - numbersAvg.forEach(function (number) { - avgSum += number; + numbersAvg.forEach(function (number) { + avgSum += number; }); - return avgSum; + return avgSum; } - return sumNumbers(numbersAvg) / numbersAvg.length + return sumNumbers(numbersAvg) / numbersAvg.length } - - - console.log(averageNumbers(numbersAvg)) - - // Level 2: Array of strings const wordsArr = ['seat', 'correspond', 'linen', 'motif', 'hole', 'smell', 'smart', 'chaos', 'fuel', 'palace']; -function averageWordLength() { } +function averageWordLength(arr) { + let lengthSum = 0 + if(arr.length === 0){ + return null; + } + for (let i=0; i