From 4ca874f78faf0b7fc8af073aa379d788f739ff4e Mon Sep 17 00:00:00 2001 From: Carlos Mateo Date: Fri, 8 Nov 2019 00:11:20 +0100 Subject: [PATCH 01/10] Adds git ignore + first iteration completed --- starter-code/src/functions-and-arrays.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/starter-code/src/functions-and-arrays.js b/starter-code/src/functions-and-arrays.js index 467e533bb..bf1ec79d1 100644 --- a/starter-code/src/functions-and-arrays.js +++ b/starter-code/src/functions-and-arrays.js @@ -1,4 +1,7 @@ // Iteration #1: Find the maximum +function maxOfTwoNumbers(a, b) { + return a > b ? a : b; +} // Iteration #2: Find longest word const words = ['mystery', 'brother', 'aviator', 'crocodile', 'pearl', 'orchard', 'crackpot']; From 7996e1d2a84b2e0379ec14368afe5ae7691419a5 Mon Sep 17 00:00:00 2001 From: Carlos Mateo Date: Fri, 8 Nov 2019 00:20:53 +0100 Subject: [PATCH 02/10] Iteration #2 completed --- .gitignore | 1 + starter-code/src/functions-and-arrays.js | 17 +++++++++++++++++ 2 files changed, 18 insertions(+) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 000000000..496ee2ca6 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.DS_Store \ No newline at end of file diff --git a/starter-code/src/functions-and-arrays.js b/starter-code/src/functions-and-arrays.js index bf1ec79d1..1a445962b 100644 --- a/starter-code/src/functions-and-arrays.js +++ b/starter-code/src/functions-and-arrays.js @@ -6,6 +6,23 @@ function maxOfTwoNumbers(a, b) { // Iteration #2: Find longest word const words = ['mystery', 'brother', 'aviator', 'crocodile', 'pearl', 'orchard', 'crackpot']; +function findLongestWord(words) { + + if (!words.length) { + return null; + } + + let longestWordCharCount = words.reduce(function (acc, e) { + return acc > e.length ? acc : e.length; + }, 0) + + return words.find(function (e) { + return e.length === longestWordCharCount; + }); +} + +findLongestWord(words); + // Iteration #3: Calculate the sum const numbers = [6, 12, 1, 18, 13, 16, 2, 1, 8, 10]; From 2337d80b481664f6f01a60431490570a3bfb6db1 Mon Sep 17 00:00:00 2001 From: Carlos Mateo Date: Fri, 8 Nov 2019 10:02:14 +0100 Subject: [PATCH 03/10] Iteratio #3 completed --- starter-code/src/functions-and-arrays.js | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/starter-code/src/functions-and-arrays.js b/starter-code/src/functions-and-arrays.js index 1a445962b..feb30d92f 100644 --- a/starter-code/src/functions-and-arrays.js +++ b/starter-code/src/functions-and-arrays.js @@ -27,6 +27,10 @@ findLongestWord(words); const numbers = [6, 12, 1, 18, 13, 16, 2, 1, 8, 10]; +numbers.reduce(function (a, b) { + return a + b; +}); + // Iteration #4: Calculate the average // Level 1: Array of numbers const numbersAvg = [2, 6, 9, 10, 7, 4, 1, 9]; From 3dc310d9ce54d33d32a62249a66033d4f6c18ceb Mon Sep 17 00:00:00 2001 From: Carlos Mateo Date: Fri, 8 Nov 2019 11:32:44 +0100 Subject: [PATCH 04/10] Wraps everything inside a function --- starter-code/src/functions-and-arrays.js | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/starter-code/src/functions-and-arrays.js b/starter-code/src/functions-and-arrays.js index feb30d92f..d719d224e 100644 --- a/starter-code/src/functions-and-arrays.js +++ b/starter-code/src/functions-and-arrays.js @@ -27,9 +27,11 @@ findLongestWord(words); const numbers = [6, 12, 1, 18, 13, 16, 2, 1, 8, 10]; -numbers.reduce(function (a, b) { - return a + b; -}); +function sumArray(numbers) { + return numbers.reduce(function (a, b) { + return a + b; + }, 0); +} // Iteration #4: Calculate the average // Level 1: Array of numbers From 449e8ce97d49fb9b650f8be80d01d0154a4dcaee Mon Sep 17 00:00:00 2001 From: Carlos Mateo Date: Fri, 8 Nov 2019 11:38:29 +0100 Subject: [PATCH 05/10] Iteration #4 completed --- starter-code/src/functions-and-arrays.js | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/starter-code/src/functions-and-arrays.js b/starter-code/src/functions-and-arrays.js index d719d224e..9c82f8f26 100644 --- a/starter-code/src/functions-and-arrays.js +++ b/starter-code/src/functions-and-arrays.js @@ -37,6 +37,14 @@ function sumArray(numbers) { // Level 1: Array of numbers const numbersAvg = [2, 6, 9, 10, 7, 4, 1, 9]; +function averageNumbers(numbers) { + if (!numbers.length) { + return null; + } + + return sumArray(numbers) / numbers.length; +} + // Level 2: Array of strings const wordsArr = [ 'seat', From 8ccba1890f43662231a11b49a803a14b76159864 Mon Sep 17 00:00:00 2001 From: Carlos Mateo Date: Fri, 8 Nov 2019 11:53:16 +0100 Subject: [PATCH 06/10] Iteration #4 level 2, completed --- starter-code/src/functions-and-arrays.js | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/starter-code/src/functions-and-arrays.js b/starter-code/src/functions-and-arrays.js index 9c82f8f26..10eb27b96 100644 --- a/starter-code/src/functions-and-arrays.js +++ b/starter-code/src/functions-and-arrays.js @@ -38,6 +38,7 @@ function sumArray(numbers) { const numbersAvg = [2, 6, 9, 10, 7, 4, 1, 9]; function averageNumbers(numbers) { + if (!numbers.length) { return null; } @@ -59,6 +60,19 @@ const wordsArr = [ 'palace' ]; +function averageWordLength(words) { + + if (!words.length) { + return null; + } + + let wordsLengths = words.map(function (el) { + return el.length; + }); + + return averageNumbers(wordsLengths); +} + // Iteration #5: Unique arrays const wordsUnique = [ 'crab', From 0e8f3139ba1af7f7777f042dd1ca54cb52f1c93e Mon Sep 17 00:00:00 2001 From: Carlos Mateo Date: Fri, 8 Nov 2019 12:55:42 +0100 Subject: [PATCH 07/10] Iteration #5 completed --- starter-code/src/functions-and-arrays.js | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/starter-code/src/functions-and-arrays.js b/starter-code/src/functions-and-arrays.js index 10eb27b96..acc3cd1c2 100644 --- a/starter-code/src/functions-and-arrays.js +++ b/starter-code/src/functions-and-arrays.js @@ -88,6 +88,19 @@ const wordsUnique = [ 'bring' ]; +function uniquifyArray(words) { + + let repeatedWords = []; + + for (let i = 0; i < words.length; i++) { + if (repeatedWords.indexOf(words[i]) === -1) { + repeatedWords.push(words[i]); + } + } + + return repeatedWords; +} + // Iteration #6: Find elements const wordsFind = [ 'machine', From 3f5367882cda6d20f08454ebe58c7fd8dfda3e58 Mon Sep 17 00:00:00 2001 From: Carlos Mateo Date: Fri, 8 Nov 2019 13:05:56 +0100 Subject: [PATCH 08/10] Iteration #6 completed --- starter-code/src/functions-and-arrays.js | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/starter-code/src/functions-and-arrays.js b/starter-code/src/functions-and-arrays.js index acc3cd1c2..f17883990 100644 --- a/starter-code/src/functions-and-arrays.js +++ b/starter-code/src/functions-and-arrays.js @@ -113,6 +113,10 @@ const wordsFind = [ 'disobedience' ]; +function doesWordExist(words, search) { + return words.includes(search); +} + // Iteration #7: Count repetition const wordsCount = [ 'machine', From af16ed75482f60366d656ebd22db87a94ec02e62 Mon Sep 17 00:00:00 2001 From: Carlos Mateo Date: Fri, 8 Nov 2019 13:16:20 +0100 Subject: [PATCH 09/10] Iteration #7 completed --- starter-code/src/functions-and-arrays.js | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/starter-code/src/functions-and-arrays.js b/starter-code/src/functions-and-arrays.js index f17883990..3cd09094a 100644 --- a/starter-code/src/functions-and-arrays.js +++ b/starter-code/src/functions-and-arrays.js @@ -132,6 +132,19 @@ const wordsCount = [ 'matter' ]; +function howManyTimes(words, search) { + + let count = 0; + + for (let i = 0; i < words.length; i++) { + if (words[i] === search) { + count++; + } + } + + return count; +} + // Iteration #8: Bonus const matrix = [ From 28f5112266c7f9566cb7f2e6178be6cd6eaba9e0 Mon Sep 17 00:00:00 2001 From: Carlos Mateo Date: Wed, 13 Nov 2019 13:00:49 +0100 Subject: [PATCH 10/10] Bonus iteration completed --- starter-code/src/functions-and-arrays.js | 109 +++++++++++++++++++++++ 1 file changed, 109 insertions(+) diff --git a/starter-code/src/functions-and-arrays.js b/starter-code/src/functions-and-arrays.js index 3cd09094a..9319a1620 100644 --- a/starter-code/src/functions-and-arrays.js +++ b/starter-code/src/functions-and-arrays.js @@ -169,3 +169,112 @@ const matrix = [ [20, 73, 35, 29, 78, 31, 90, 1, 74, 31, 49, 71, 48, 86, 81, 16, 23, 57, 5, 54], [1, 70, 54, 71, 83, 51, 54, 69, 16, 92, 33, 48, 61, 43, 52, 1, 89, 19, 67, 48] ]; + +function greatestProduct(matrix) { + + let adjacentNumbers = 4, + products = [], + limitX, + limitY; + + if (areAllElementsEqual(matrix)) { + let product = getHorizontalVector(0, 0, matrix, adjacentNumbers - 1); + return product[product.length - 1]; + } + + for (let x = 0; x < matrix.length; x++) { + for (let y = 0; y < matrix.length; y++) { + + limitY = y + adjacentNumbers - 1; + + if (limitY <= matrix[x].length - 1) { + products.push(getHorizontalVector(x, y, matrix, limitY)); + } + + limitX = x + adjacentNumbers - 1; + + if (limitX <= matrix[y].length - 1) { + products.push(getVerticalVector(x, y, matrix, limitX)); + } + + if (limitY <= matrix[x].length - 1 && limitX <= matrix[y].length - 1) { + products.push(getDiagonalVector(x, y, matrix, adjacentNumbers - 1)); + } + + } + } + + return getGreatestProduct(products); +} + +function areAllElementsEqual(matrix) { + + let firstMatrixElement = matrix[0][0]; + let elementsAreEqual = true; + + for (let x = 0; x < matrix.length; x++) { + for (let y = 0; y < matrix.length; y++) { + if (matrix[x][y] !== firstMatrixElement) { + return !elementsAreEqual; + } + } + } + + return elementsAreEqual; +} + +function getGreatestProduct(products) { + + let greatestProduct = products.reduce(function (a, b) { + return a > b[b.length - 1] ? a : b[b.length - 1]; + }, 0); + + return products.filter(function (el) { + return el[el.length - 1] === greatestProduct + })[0]; +} + +function getHorizontalVector(x, y, matrix, limit) { + + let vector = []; + + for (let i = y; i <= limit; i++) { + vector.push(matrix[x][i]); + } + + vector.push(getVectorProduct(vector)); + + return vector; +} + +function getVerticalVector(x, y, matrix, limit) { + + let vector = []; + + for (let i = x; i <= limit; i++) { + vector.push(matrix[i][y]); + } + + vector.push(getVectorProduct(vector)); + + return vector; +} + +function getDiagonalVector(x, y, matrix, limit) { + + let vector = []; + + for (let i = 0; i <= limit; i++) { + vector.push(matrix[x + i][y + i]); + } + + vector.push(getVectorProduct(vector)); + + return vector; +} + +function getVectorProduct(vector) { + return vector.reduce(function (a, b) { + return a * b; + }) +} \ No newline at end of file