diff --git a/src/functions-and-arrays.js b/src/functions-and-arrays.js index 3a7dbec41..5e9ce3956 100644 --- a/src/functions-and-arrays.js +++ b/src/functions-and-arrays.js @@ -1,20 +1,38 @@ // Iteration #1: Find the maximum -function maxOfTwoNumbers() {} - +function maxOfTwoNumbers(x,y) { + if(x>y) return(x); + else if (x longestWord.length) { + longestWord = arr[i]; + } + } + + return longestWord; +} // Iteration #3: Calculate the sum const numbers = [6, 12, 1, 18, 13, 16, 2, 1, 8, 10]; -function sumNumbers() {} - +function sumNumbers(arr) { + if (!arr.length) return 0; + return arr.reduce((sum, num) => sum + num, 0); +} // Iteration #3.1 Bonus: @@ -22,21 +40,38 @@ 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(arr) { + if (!arr.length) return null; + return sumNumbers(arr) / arr.length; +} + +//esto lo entiendo como la suma de los numeros entre la division +// osea 48 total entre 8 =6 de promedio. + + // Level 2: Array of strings const wordsArr = ['seat', 'correspond', 'linen', 'motif', 'hole', 'smell', 'smart', 'chaos', 'fuel', 'palace']; -function averageWordLength() { } +function averageWordLength(arr){ + if (!arr.length) return null; + if (arr.length === 1) return arr[0].length; + return arr.reduce((sum, word) => sum + word.length, 0) / arr.length; + +} // Bonus - Iteration #4.1 function avg() {} + // Iteration #5: Unique arrays const wordsUnique = [ 'crab', @@ -52,14 +87,32 @@ const wordsUnique = [ 'bring' ]; -function uniquifyArray() {} +function uniquifyArray(arr){ + if (!arr.length) return null; + return [...new Set(arr)]; + + +} + + +//VOY ACA // + // Iteration #6: Find elements const wordsFind = ['machine', 'subset', 'trouble', 'starting', 'matter', 'eating', 'truth', 'disobedience']; -function doesWordExist() {} +function doesWordExist(arr, word) { + if (!arr.length) return null; + if (arr.length === 1 && arr[0] === word) { + return true; + } + if (arr.includes(word)) { + return true; + } + return false; +} @@ -78,7 +131,14 @@ const wordsCount = [ 'matter' ]; -function howManyTimes() {} +function howManyTimes(arr, word) { + if (!arr.length) return 0; + + const Count = arr.filter(item => item === word).length; + + return Count; + +}