From ce827647f8bc995034206087f0573fb920557a9b Mon Sep 17 00:00:00 2001 From: 9sheila Date: Mon, 8 May 2023 21:25:03 +0200 Subject: [PATCH 1/2] first commit --- src/functions-and-arrays.js | 1 + 1 file changed, 1 insertion(+) diff --git a/src/functions-and-arrays.js b/src/functions-and-arrays.js index 3a7dbec41..9e173e086 100644 --- a/src/functions-and-arrays.js +++ b/src/functions-and-arrays.js @@ -17,6 +17,7 @@ function sumNumbers() {} + // Iteration #3.1 Bonus: function sum() {} From b61c5271a9bf1462d53ddefb3ef7ac69da0f4752 Mon Sep 17 00:00:00 2001 From: 9sheila Date: Fri, 12 May 2023 21:15:41 +0200 Subject: [PATCH 2/2] primera aprte done --- src/functions-and-arrays.js | 92 +++++++++++++++++++++++++++++++------ 1 file changed, 77 insertions(+), 15 deletions(-) diff --git a/src/functions-and-arrays.js b/src/functions-and-arrays.js index 9e173e086..9f2061565 100644 --- a/src/functions-and-arrays.js +++ b/src/functions-and-arrays.js @@ -1,25 +1,54 @@ // Iteration #1: Find the maximum -function maxOfTwoNumbers() {} - - +function maxOfTwoNumbers(num1, num2) { + if (num1 > num2) { + return num1; + } + 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 = ''; + for (let i = 0; i < words.length; i++) { + if (words[i].length > longestWord.length) { + longestWord = words[i]; + } + } + return longestWord +} // Iteration #3: Calculate the sum const numbers = [6, 12, 1, 18, 13, 16, 2, 1, 8, 10]; -function sumNumbers() {} +function sumNumbers(numArr) { + if (numArr.length === 0){ + return 0 + } + /*let sum = 0; +for (i = 0; i < numArr.length; i++){ +sum += numArr[i]*/ +let sum = 0 + numArr.forEach(function(num) { + sum += num + }); + return sum + } + + + + // Iteration #3.1 Bonus: -function sum() {} +function sum() { } @@ -27,16 +56,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 sum2 = 0 + for (let i=0; i < numbersAvg.length; i++){ + sum2 += numbersAvg[i] + } + const average = sum2/numbersAvg.length; + return average; +} // 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 sum3 = 0 + for (let i=0; i < wordsArr.length; i++){ + sum3 += wordsArr[i].length + } + const average = sum3/wordsArr.length; + return average; +} + + // Bonus - Iteration #4.1 -function avg() {} +function avg() { } // Iteration #5: Unique arrays const wordsUnique = [ @@ -53,14 +104,25 @@ const wordsUnique = [ 'bring' ]; -function uniquifyArray() {} +function uniquifyArray(wordsUnique) { + if(wordsUnique.length === 0){ + return null +} + let uniqueArr = [] + wordsUnique.forEach(function(singleW){ + if (!uniqueArr.includes (singleW)){ + uniqueArr.push(singleW) + } + }) + return uniqueArr + } // Iteration #6: Find elements const wordsFind = ['machine', 'subset', 'trouble', 'starting', 'matter', 'eating', 'truth', 'disobedience']; -function doesWordExist() {} +function doesWordExist() { } @@ -79,7 +141,7 @@ const wordsCount = [ 'matter' ]; -function howManyTimes() {} +function howManyTimes() { } @@ -107,7 +169,7 @@ 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() { }