diff --git a/README.md b/README.md index 5d11b9e6e..81bf62d26 100644 --- a/README.md +++ b/README.md @@ -93,7 +93,7 @@ Define a function `maxOfTwoNumbers` that takes two numbers as arguments and retu ## Finding Longest Word -Write a function `findLongestWord` that takes an array of words and returns the length of the longest one. If there are 2 with the same length, it should return the first occurrence. +Write a function `findLongestWord` that takes an array of words and returns the longest one. If there are 2 with the same length, it should return the first occurrence. **Starter Code** diff --git a/starter-code/src/functions-and-arrays.js b/starter-code/src/functions-and-arrays.js index 2fcd81e6e..8ff1d054c 100644 --- a/starter-code/src/functions-and-arrays.js +++ b/starter-code/src/functions-and-arrays.js @@ -1,4 +1,10 @@ // Find the maximum +function maxOfTwoNumbers(a,b) { + if(a > b) { + return a + } + return b +} // Finding Longest Word var words = [ @@ -11,14 +17,44 @@ var words = [ 'crackpot' ]; +function wordsArrayLength(words){ + let temp = []; + words.forEach((str,i) => { + temp[i] = str.length + }); + return temp +} + +function findLongestWord(words) { + if(words.length === 1) return words[0] + + temp = wordsArrayLength(words); + + let n = temp[1]; + for(let i=0; i < temp.length; i++){ + if(n<=temp[i]){ + n = temp[i]; + } + } + return (words[temp.indexOf(n)]); +} // Calculating a Sum var numbers = [6, 12, 1, 18, 13, 16, 2, 1, 8, 10]; -// Calculate the Average +function sumArray(numbers){ + if(numbers.length === 0) return 0; + return numbers.reduce((acc, val) => acc+val) +} +// Calculate the Average var numbersAvg = [2, 6, 9, 10, 7, 4, 1, 9]; +function averageNumbers(numbersAvg){ + if(numbersAvg.length === 0) return undefined; + return sumArray(numbersAvg)/numbersAvg.length +} + // Array of Strings var wordsArr = [ 'seat', @@ -33,6 +69,11 @@ var wordsArr = [ 'palace' ]; +function averageWordLength(wordsArr){ + temp = wordsArrayLength(wordsArr) + return averageNumbers(temp) +} + // Unique Arrays var wordsUnique = [ 'crab', @@ -48,6 +89,11 @@ var wordsUnique = [ 'bring' ]; +function uniquifyArray(wordsUnique){ + if(wordsUnique.length ===0) return undefined; + return wordsUnique.filter((word, i, arr) => arr.indexOf(word) === i); +} + // Finding Elements var wordsFind = [ 'machine', @@ -60,6 +106,10 @@ var wordsFind = [ 'disobedience' ]; +function doesWordExist(wordsFind, word){ + return wordsFind.includes(word); +} + // Counting Repetion var wordsCount = [ 'machine', @@ -74,6 +124,17 @@ var wordsCount = [ 'disobedience', 'matter' ]; + +function howManyTimes(wordsCount, search) { + if(wordsCount.length === 0) return false + let counter = 0; + wordsCount.forEach((word, i) => { + if(search===word){ + counter += 1; + } + }) + return counter; +} // Bonus Quest var matrix = [ @@ -98,3 +159,48 @@ var 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 prod = 0, + m = [1,1,1,1]; + let multMatrix = []; + + for(let i=0; i < matrix.length; i++){ + for(let j=0; j < matrix[i].length; j++){ + //UP BLOCK + if(matrix[i-1]) { + m[0] = matrix[i-1][j] } + + //DOWN BLOCK + if(matrix[i+1]) { + m[1] = matrix[i+1][j]; + } + + //RIGHT BLOCK + if(matrix[i][j+1]) { + m[2] = matrix[i][j+1]; + } + + //LEFT BLOCK + if(matrix[i][j-1]) { + m[3] = matrix[i][j-1]; + } + + console.log(m) + + //MULTIPLY BLOCK + prod = m[0]*m[1]*m[2]*m[3]; + multMatrix.push(prod); + m = [1,1,1,1]; + } + } + console.log(multMatrix); + console.log(Math.max(...multMatrix)); + return Math.max(...multMatrix) +} + +greatestProduct([ +[2,2,2,2,2], +[2,2,2,2,2], +[2,2,2,2,2], +[2,2,2,2,2]]); \ No newline at end of file diff --git a/starter-code/tests/FunctionsAndArraysSpec.js b/starter-code/tests/FunctionsAndArraysSpec.js index 8fb25ff65..67a3b8bad 100644 --- a/starter-code/tests/FunctionsAndArraysSpec.js +++ b/starter-code/tests/FunctionsAndArraysSpec.js @@ -170,7 +170,7 @@ describe('Counting Repetion - howManyTimes', function () { }); it('returns false with an empty array', function () { - expect(howManyTimes([])).toBe(undefined); + expect(howManyTimes([])).toBe(false); }); it('returns one when the word appears only one time on the array', function () {