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 467e533bb..9319a1620 100644 --- a/starter-code/src/functions-and-arrays.js +++ b/starter-code/src/functions-and-arrays.js @@ -1,16 +1,51 @@ // 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']; +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]; +function sumArray(numbers) { + return numbers.reduce(function (a, b) { + return a + b; + }, 0); +} + // Iteration #4: Calculate the average // 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', @@ -25,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', @@ -40,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', @@ -52,6 +113,10 @@ const wordsFind = [ 'disobedience' ]; +function doesWordExist(words, search) { + return words.includes(search); +} + // Iteration #7: Count repetition const wordsCount = [ 'machine', @@ -67,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 = [ @@ -91,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