From c0e48acfa2427550b06f3c8fa8c2133e70885722 Mon Sep 17 00:00:00 2001 From: Carlos HM Date: Tue, 22 Oct 2019 23:56:45 -0500 Subject: [PATCH 1/2] Exercise Done --- starter-code/src/functions-and-arrays.js | 94 +++++++++++++++++++++++- 1 file changed, 93 insertions(+), 1 deletion(-) diff --git a/starter-code/src/functions-and-arrays.js b/starter-code/src/functions-and-arrays.js index c1f489158..8dbb6b1d3 100644 --- a/starter-code/src/functions-and-arrays.js +++ b/starter-code/src/functions-and-arrays.js @@ -1,4 +1,16 @@ // Find the maximum +const maxOfTwoNumbers = (num1, num2) => { + if(num1 > num2) { + return num1; + } else { + return num2; + } +} + +maxOfTwoNumbers(2,1); +maxOfTwoNumbers(1,2); +maxOfTwoNumbers(2,2); + // Finding Longest Word const words = [ @@ -11,14 +23,61 @@ const words = [ 'crackpot' ]; +const findLongestWord = arr =>{ + if(!arr.length) { + return null; + } else if (arr.length === 1){ + return arr[0]; + } + + + arr.array.forEach(element => { + console.log(element); + if (element.length === arr.length) { + return element; + } + + console.log() + }); +} + // Calculating a Sum const numbers = [6, 12, 1, 18, 13, 16, 2, 1, 8, 10]; -// Calculate the Average +const sumArray = arr => { + let sum = 0; + + if (!arr.length) { + return 0; + } + + for (let i = 0; i < arr.length; i++) { + sum += arr[i]; + } + return sum; +} + +sumArray(numbers); + +// Calculate the Average const numbersAvg = [2, 6, 9, 10, 7, 4, 1, 9]; +const averageNumbers = arr => { + let sum = 0; + + if (!arr.length) { + return null; + } + for (let i = 0; i < arr.length; i++) { + sum += arr[i]; + } + return sum / arr.length; +}; + +averageNumbers(numbersAvg); + // Array of Strings const wordsArr = [ 'seat', @@ -33,6 +92,19 @@ const wordsArr = [ 'palace' ]; +const averageWordLength = arr => { +let averageWord = 0; + + if(!arr.length) { + return null; + } + + for(let i = 0; i < arr.length; i++) { + averageWord += arr[i].length; + } + return averageWord / arr.length; +}; + // Unique Arrays const wordsUnique = [ 'crab', @@ -48,6 +120,26 @@ const wordsUnique = [ 'bring' ]; +const uniquifyArray = arr => { + if (!arr.length) { + return []; + } + + for(let i = 0; i < arr.length; i++) { + //console.log(arr[i]); + for(let j = 0; j < arr.length - 1; j++) { + //console.log(arr[i]); + //console.log('Segunda ' + arr[i+1]); + if(arr[i] === arr[j]) { + arr.splice(arr[j],1); + } + } + } + +} + +//uniquifyArray(wordsUnique); + // Finding Elements const wordsFind = [ 'machine', From 7fd4d0f962d9450d042bc6645e32a13b2a5f5daa Mon Sep 17 00:00:00 2001 From: Carlos HM Date: Wed, 23 Oct 2019 09:37:03 -0500 Subject: [PATCH 2/2] Ejercicio Actualizado --- starter-code/src/functions-and-arrays.js | 59 ++++++++++++++++++++---- 1 file changed, 51 insertions(+), 8 deletions(-) diff --git a/starter-code/src/functions-and-arrays.js b/starter-code/src/functions-and-arrays.js index 8dbb6b1d3..d3e847f82 100644 --- a/starter-code/src/functions-and-arrays.js +++ b/starter-code/src/functions-and-arrays.js @@ -24,23 +24,26 @@ const words = [ ]; const findLongestWord = arr =>{ + let longestWord = ''; + if(!arr.length) { return null; } else if (arr.length === 1){ return arr[0]; } - - arr.array.forEach(element => { - console.log(element); - if (element.length === arr.length) { - return element; + for (let i = 0; i < arr.length; i++) { + for (let j= 0; j < arr.length - 1 ; j++){ + if(arr[j].length > arr[i].length) { + longestWord = arr[j]; + } + } } - - console.log() - }); + return longestWord; } +findLongestWord(words); + // Calculating a Sum const numbers = [6, 12, 1, 18, 13, 16, 2, 1, 8, 10]; @@ -152,6 +155,27 @@ const wordsFind = [ 'disobedience' ]; + const doesWordExist = (arr, word) => { + if(!arr.length) { + return false; + } + if(arr[0] === word) { + return true; + } + for (let i = 0; i < arr.length; i++){ + if(arr[i] === word) { + return true; + } else { + return false; + } + } + } + + + doesWordExist(wordsFind, 'machine') + + + // Counting Repetition const wordsCount = [ 'machine', @@ -167,6 +191,25 @@ const wordsCount = [ 'matter' ]; + +const howManyTimes = (arr, word) => { + let wordTimes = 0; + + if(!arr.length) { + return 0; + } + + for(let i = 0; i < arr.length; i++) { + if(arr[i] === word) { + wordTimes++; + } + } + return wordTimes; +} + +howManyTimes(wordsCount, 'matter'); + + // Bonus const matrix = [